aboutsummaryrefslogtreecommitdiff
path: root/aboutit/src/main/java/com/snilius/aboutit/AboutIt.java
blob: 2bc29c128904731f0005b5edb439ed42f0d09f16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package com.snilius.aboutit;

import android.app.Activity;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.text.util.Linkify;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <h1>AboutIt</h1>
 * A About-page creator
 *
 * <pre>
 * {@code
 * new AboutIt(this).app(R.string.app_name)
 *     .buildInfo(BuildConfig.DEBUG, BuildConfig.VERSION_CODE, BuildConfig.VERSION_NAME)
 *     .copyright("Example Business")
 *     .libLicense("AboutIt", "Victor Häggqvist", L.AP2, "https://github.com/victorhaggqvist/aboutit")
 *     .toTextView(R.id.about_text);
 * }
 * </pre>
 *
 * @author Victor Häggqvist
 * @since 12/29/14
 * @version 1.0
 */
public class AboutIt {

    private String copyright = null;
    private int year = 0;
    private int endYear = 0;
    private List<Lib> libs = new ArrayList<>();
    private Activity activity;
    private String appName = null;
    private boolean debug;
    private int versionCode;
    private String versionName;
    private String releaseName = null;
    private String description = null;

    /**
     * Create a page generator
     * @param activity The about page activity
     */
    public AboutIt(Activity activity) {

        this.activity = activity;
    }

    /**
     * Generate text and put in @ref{about_text}
     * @param about_text Resource it of destination TextView
     */
    @SuppressWarnings("StringConcatenationInsideStringBufferAppend")
    public void toTextView(@IdRes int about_text) {
        TextView out = (TextView) activity.findViewById(about_text);
        out.setAutoLinkMask(Linkify.WEB_URLS);

        endYear = endYear();

        StringBuilder sb = new StringBuilder();
        if (appName != null)
            sb.append(appName+" v"+getVersionString()+"\n");

        if (copyright != null) {
            sb.append("Copyright (c) ");
            if (year != 0)
                sb.append(year + (endYear != 0 ? " - " + endYear : "") + " ");
            sb.append(copyright + "\n\n");
        }

        if (description != null)
            sb.append(description+"\n\n");

        // Sort library list alphabetically by name
        Collections.sort(libs, new Comparator<Lib>() {
            @Override
            public int compare(Lib lhs, Lib rhs) {
                return lhs.name.compareTo(rhs.name);
            }
        });

        for(Lib l:libs){
            sb.append(l.byLine() + "\n");
        }

        out.setText(sb.toString());
    }

    /**
     * Get a baked version string.
     * Version string is build from build info and release name
     * It will look something like '1.42 (42-debug)'
     * @return baked version string
     */
    public String getVersionString() {
        releaseName = "-" + (releaseName != null ? releaseName : (debug?"debug":""));
        if (releaseName.equals("-"))
            return versionName+" ("+versionCode+")";
        else
            return versionName+" ("+versionCode+releaseName+")";
    }

    /**
     * Determine what end year to show
     * @return year to show
     */
    private int endYear() {
        if (endYear != 0) {
            return endYear;
        } else {
            Calendar now = Calendar.getInstance();
            int yearNow = now.get(Calendar.YEAR);
            if (year == yearNow)
                return 0;
            else
                return yearNow;
        }
    }

    /**
     * Get String by id
     * @param stringId String id
     * @return String
     */
    private String s(@StringRes int stringId) {
        return activity.getString(stringId);
    }

    /**
     * Copyright name
     * @param copyright Name
     */
    public AboutIt copyright(String copyright){
        this.copyright = copyright;
        return this;
    }

    /**
     * Start copyright year.
     * If there is no start year no year will be displayed at all.
     * @param year Year
     */
    public AboutIt year(int year) {
        this.year = year;
        return this;
    }

    /**
     * Add a library to the list
     * @param name      Name of Library
     * @param author    Author of library
     * @param license   Library license, defined by L
     * @param url       Url to or otherwise reference to library
     * @see L
     */
    public AboutIt libLicense(String name, String author, L license, String url) {
        libs.add(new Lib(name, author, license, url));
        return this;
    }

    /**
     * Add a library to the list
     * @param library A library build with LibBuilder
     */
    public AboutIt libLicense(Lib library) {
        libs.add(library);
        return this;
    }

    /**
     * App name to display
     * @param stringResource A string resource id
     */
    public AboutIt app(@StringRes int stringResource) {
        this.appName = s(stringResource);
        return this;
    }

    /**
     * App name to display
     * @see #app(int)
     */
    public AboutIt app(String appName) {
        this.appName = appName;
        return this;
    }

    /**
     * Set a custom release name. Override the default name eg. beta
     * @param releaseName The release name
     */
    public AboutIt release(String releaseName){
        this.releaseName = releaseName;
        return this;
    }

    /**
     * App build info. To be used in conjunction with the BuildConfig class
     * {@code .buildInfo(BuildConfig.DEBUG, BuildConfig.VERSION_CODE, BuildConfig.VERSION_NAME)}
     *
     * @param debug         If is debug build
     * @param versionCode   Version code
     * @param versionName   Version name
     */
    public AboutIt buildInfo(boolean debug, int versionCode, String versionName) {
        this.debug = debug;
        this.versionCode = versionCode;
        this.versionName = versionName;
        return this;
    }

    /**
     * A longer description
     * @param stringResource A string resource id
     */
    public AboutIt description(@StringRes int stringResource) {
        description = s(stringResource);
        return this;
    }

    /**
     * A longer description
     * @param description The description
     * @see #description(int)
     */
    public AboutIt description(String description) {
        this.description = description;
        return this;
    }

    /**
     * Holder for libraries
     */
    static class Lib {

        final String name;
        final String author;
        final LicenseBase license;
        final String url;

        public Lib(String name, String author, LicenseBase license, String url) {

            this.name = name;
            this.author = author;
            this.license = license;
            this.url = url;
        }

        String byLine() {
            return name + " by " + author + " under " + license.display() + ", " + url;
        }
    }
}