Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;

import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;

import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;

import android.util.Log;

public class Main {
    public static final String TAG = "ChangelogHelper";
    private static final String PREFS_LAST_RUN = "lastrun";

    public static void showChangelog(final Context context, final String title, final String appname,
            final int resChanges, final int resNotes) {
        final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
        final String v0 = p.getString(PREFS_LAST_RUN, "");
        String v1 = null;
        try {
            v1 = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (NameNotFoundException e) {
            Log.e(TAG, "package not found: " + context.getPackageName(), e);
        }
        p.edit().putString(PREFS_LAST_RUN, v1).commit();
        if (v0.length() == 0) {
            Log.d(TAG, "first boot, skip changelog");
            // return;
        }
        if (v0.equals(v1)) {
            Log.d(TAG, "no changes");
            return;
        }

        String[] changes = context.getResources().getStringArray(resChanges);
        String[] notes = resNotes > 0 ? context.getResources().getStringArray(resNotes) : null;

        final SpannableStringBuilder sb = new SpannableStringBuilder();
        for (String s : notes) {
            SpannableString ss = new SpannableString(s + "\n");
            int j = s.indexOf(":");
            if (j > 0) {
                if (!TextUtils.isEmpty(s)) {
                    ss.setSpan(new StyleSpan(Typeface.BOLD), 0, j, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
            sb.append(ss);
            sb.append("\n");
        }
        if (notes != null && notes.length > 0) {
            sb.append("\n");
        }
        for (String s : changes) {
            s = appname + " " + s.replaceFirst(": ", ":\n* ").replaceAll(", ", "\n* ") + "\n";
            SpannableString ss = new SpannableString(s);
            int j = s.indexOf(":");
            if (j > 0) {
                ss.setSpan(new StyleSpan(Typeface.BOLD), 0, j, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            sb.append(ss);
            sb.append("\n");
        }
        sb.setSpan(new RelativeSizeSpan(0.75f), 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        changes = null;
        notes = null;

        AlertDialog.Builder b = new AlertDialog.Builder(context);
        b.setTitle(title);
        b.setMessage(sb);
        b.setCancelable(true);
        b.setPositiveButton(android.R.string.ok, null);
        b.show();
    }
}