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
|
package eu.siacs.conversations.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.services.UpdaterWebService;
public class UpdaterActivity extends Activity {
private static final String LOG_TAG = Config.LOGTAG + "AppUpgrade";
private MyWebReceiver receiver;
private int versionCode = 0;
String appURI = "";
private DownloadManager downloadManager;
private long downloadReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_updater);
//Overall information about the contents of a package
//This corresponds to all of the information collected from AndroidManifest.xml.
PackageInfo pInfo = null;
try {
pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
//get the app version Name for display
String version = pInfo.versionName;
//get the app version Code for checking
versionCode = pInfo.versionCode;
//display the current version in a TextView
TextView currentversionText = (TextView) findViewById(R.id.current_versionName);
currentversionText.setText(getText(R.string.current_version) + version);
//Broadcast receiver for our Web Request
IntentFilter filter = new IntentFilter(MyWebReceiver.PROCESS_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MyWebReceiver();
registerReceiver(receiver, filter);
//Broadcast receiver for the download manager
filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, filter);
//check of internet is available before making a web service request
if(isNetworkAvailable(this)){
Intent msgIntent = new Intent(this, UpdaterWebService.class);
msgIntent.putExtra(UpdaterWebService.REQUEST_STRING, Config.UPDATE_URL);
startService(msgIntent);
}
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
*/
@Override
public void onDestroy() {
//unregister your receivers
this.unregisterReceiver(receiver);
this.unregisterReceiver(downloadReceiver);
super.onDestroy();
}
//check for internet connection
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
Log.v(LOG_TAG, String.valueOf(i));
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
Log.v(LOG_TAG, "connected!");
return true;
}
}
}
}
return false;
}
//broadcast receiver to get notification when the web request finishes
public class MyWebReceiver extends BroadcastReceiver {
public static final String PROCESS_RESPONSE = "eu.siacs.conversations.intent.action.PROCESS_RESPONSE";
@Override
public void onReceive(Context context, Intent intent) {
String reponseMessage = intent.getStringExtra(UpdaterWebService.RESPONSE_MESSAGE);
Log.v(LOG_TAG, reponseMessage);
//parse the JSON response
JSONObject responseObj;
try {
responseObj = new JSONObject(reponseMessage);
boolean success = responseObj.getBoolean("success");
//if the reponse was successful check further
if(success){
//get the latest version from the JSON string
int latestVersionCode = responseObj.getInt("latestVersionCode");
String latestVersion = responseObj.getString("latestVersion");
//display the new version in a TextView
TextView versionText = (TextView) findViewById(R.id.versionName);
versionText.setText(getText(R.string.new_version) + latestVersion);
//get the lastest application URI from the JSON string
appURI = responseObj.getString("appURI");
//check if we need to upgrade?
if(latestVersionCode > versionCode){
//oh yeah we do need an upgrade, let the user know send an alert message
AlertDialog.Builder builder = new AlertDialog.Builder(UpdaterActivity.this);
builder.setMessage(R.string.update_available)
.setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
//if the user agrees to upgrade
public void onClick(DialogInterface dialog, int id) {
//start downloading the file using the download manager
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(appURI);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
request.setTitle("Conversations Update");
request.setDestinationInExternalFilesDir(UpdaterActivity.this, Environment.DIRECTORY_DOWNLOADS,"MyAndroidApp.apk");
downloadReference = downloadManager.enqueue(request);
}
})
.setNegativeButton(R.string.remind_later, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
//show the alert message
builder.create().show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//broadcast receiver to get notification about ongoing downloads
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//check if the broadcast message is for our Enqueued download
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if(downloadReference == referenceId){
Log.v(LOG_TAG, "Downloading of the new app version complete");
//start the installation of the latest version
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setDataAndType(downloadManager.getUriForDownloadedFile(downloadReference),
"application/vnd.android.package-archive");
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(installIntent);
}
}
};
}
|