If you think the Android project caddisfly-app-camera listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) TernUp Research Labs/*fromwww.java2s.com*/
*
* This file is part of Caddisfly
*
* Caddisfly is free software: you can redistribute it and modify it under the terms of
* the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
* either version 3 of the License or any later version.
*
* Caddisfly 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 Affero General Public License included below for more details.
*
* The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
*/package com.ternup.caddisfly.util;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import com.ternup.caddisfly.R;
import com.ternup.caddisfly.app.Globals;
import java.util.Calendar;
publicclass UpdateCheckTask extends AsyncTask<Void, Void, Void> {
privatestatic UpdateChecker checker;
privatefinal Context mContext;
privatefinalboolean mBackground;
privatefinalboolean mPreviousVersion;
private ProgressDialog progressDialog;
public UpdateCheckTask(Context context, boolean background, boolean previousVersion) {
mContext = context;
mBackground = background;
mPreviousVersion = previousVersion;
}
publicstatic String getPreviousVersion(Context context) {
try {
int version = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0).versionCode;
return String.valueOf(version - 1);
} catch (PackageManager.NameNotFoundException e) {
return"";
}
}
@Override
protectedvoid onPreExecute() {
super.onPreExecute();
if (NetworkUtils.isOnline(mContext)) {
checker = new UpdateChecker(mContext, false);
if (mPreviousVersion) {
String url = Globals.UPDATE_URL.replace(".apk", "_prev" + ".apk");
checker.downloadAndInstall(url, true);
this.cancel(true);
return;
} else {
if (!mBackground) {
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(mContext.getString(R.string.checkingForUpdates));
progressDialog.setCancelable(false);
progressDialog.show();
}
}
} else {
this.cancel(true);
if (!mBackground) {
NetworkUtils.checkInternetConnection(mContext);
}
}
}
@Override
protected Void doInBackground(Void... voids) {
boolean updateAvailable = PreferencesUtils
.getBoolean(mContext, R.string.updateAvailable, false);
if (!updateAvailable) {
if (checker.checkForUpdateByVersionCode(Globals.UPDATE_CHECK_URL)) {
PreferencesUtils.setLong(mContext, R.string.lastUpdateCheck,
Calendar.getInstance().getTimeInMillis());
if (checker.isUpdateAvailable()) {
PreferencesUtils.setBoolean(mContext, R.string.updateAvailable, true);
}
}
} else {
checker.setUpdateAvailable();
}
return null;
}
@Override
protectedvoid onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (progressDialog != null) {
progressDialog.dismiss();
}
if (checker.isUpdateAvailable()) {
AlertUtils.askQuestion(mContext, R.string.appUpdate, R.string.askForUpdate,
new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialogInterface, int i) {
checker.downloadAndInstall(Globals.UPDATE_URL, false);
PreferencesUtils.removeKey(mContext, R.string.updateAvailable);
}
}, null
);
} else {
if (!mBackground) {
AlertUtils.showMessage(mContext, R.string.appName, R.string.alreadyHaveLatest);
}
}
}
}