-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWhatsNewActivity.java
More file actions
164 lines (145 loc) · 4.74 KB
/
WhatsNewActivity.java
File metadata and controls
164 lines (145 loc) · 4.74 KB
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
/* Copyright (c) 2010-2011 Pierre LEVY androidsoft.org
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.androidsoft.utils.ui;
import android.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
/**
* What's New Activity - Display a dialog box for the very first run and for the
* first run of each update.
* @author Pierre Levy
*/
public abstract class WhatsNewActivity extends NoTitleActivity
{
public abstract int getFirstRunDialogTitleRes();
public abstract int getFirstRunDialogMsgRes();
public abstract int getWhatsNewDialogTitleRes();
public abstract int getWhatsNewDialogMsgRes();
private static final String KEY_VERSION = "version";
private static final int DEFAULT_VERSION = -1;
/**
* {@inheritDoc }
*/
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
checkLastVersion();
}
/**
* Checks if running a new update or first run
*/
private void checkLastVersion()
{
final int lastVersion = getSavedVersion();
final int currentVersion = getVersionNumber();
if (lastVersion < currentVersion)
{
int resTitle;
String resMessage;
if (lastVersion == DEFAULT_VERSION)
{
// This is a new install
resTitle = getFirstRunDialogTitleRes();
resMessage = getFirstRunDialogMsgString();
}
else
{
// This is an upgrade.
resTitle = getWhatsNewDialogTitleRes();
resMessage = getWhatsNewDialogMsgString();
}
// show what's new message
saveVersion(currentVersion);
showWhatsNewDialog(resTitle, resMessage);
}
}
public String getFirstRunDialogMsgString() {
return getResources().getString(getFirstRunDialogMsgRes());
}
public String getWhatsNewDialogMsgString() {
return getResources().getString(getWhatsNewDialogMsgRes());
}
/**
* Gets a version number stored in preferences
* @return The version number
*/
private int getSavedVersion()
{
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
return prefs.getInt(KEY_VERSION, DEFAULT_VERSION);
}
/**
* Save a version number into preferences
* @param version The version number
*/
private void saveVersion(int version)
{
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt(KEY_VERSION, version);
editor.commit();
}
/**
* Show teh What's new dialog
* @param title The dialog's title
* @param message The dialog's message
*/
private void showWhatsNewDialog(int title, String message)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setIcon(R.drawable.ic_menu_info_details);
builder.setMessage(message);
builder.setPositiveButton(getString(R.string.ok),
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
/**
* Gets the version number
* @return The version number
*/
public int getVersionNumber()
{
int versionNo = 0;
PackageInfo pInfo = null;
try
{
pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
}
catch (NameNotFoundException e)
{
pInfo = null;
}
if (pInfo != null)
{
versionNo = pInfo.versionCode;
}
return versionNo;
}
}