-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
App version #19
Merged
Merged
App version #19
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6c78511
Display current app version on the top of the app
norkans7 1510035
Refresh app version on Home activity create
norkans7 06ed4d6
Move app version on the bottom of content layout
norkans7 0af17a9
Download messsage packs from the server
norkans7 6bdad88
Use message pack version to specify the version we need from the server
norkans7 16510b4
Add paths xml file
norkans7 386e404
Use broadcast receiver on download complete
norkans7 2a1f1a7
Refresh installed packs on HomeActivity resume
norkans7 0e744e5
Move app version to gradle file
norkans7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,27 +19,36 @@ | |
package io.rapidpro.androidchannel; | ||
|
||
import android.app.Application; | ||
import android.app.DownloadManager; | ||
import android.app.NotificationChannel; | ||
import android.app.NotificationManager; | ||
import android.app.PendingIntent; | ||
import android.app.job.JobInfo; | ||
import android.app.job.JobScheduler; | ||
import android.content.BroadcastReceiver; | ||
import android.content.ComponentName; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.content.SharedPreferences; | ||
import android.content.pm.ApplicationInfo; | ||
import android.content.pm.PackageInfo; | ||
import android.content.pm.PackageManager; | ||
import android.database.Cursor; | ||
import android.net.ConnectivityManager; | ||
import android.net.Uri; | ||
import android.os.AsyncTask; | ||
import android.os.Build; | ||
import android.os.Environment; | ||
import android.preference.PreferenceManager; | ||
import android.provider.CallLog.Calls; | ||
import android.provider.Telephony; | ||
import android.support.annotation.RequiresApi; | ||
import android.support.v4.app.NotificationCompat; | ||
import android.support.v4.app.TaskStackBuilder; | ||
import android.support.v4.content.FileProvider; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
|
@@ -50,6 +59,9 @@ | |
import io.rapidpro.androidchannel.payload.MTTextMessage; | ||
import io.rapidpro.androidchannel.payload.ResetCommand; | ||
|
||
import static android.support.v4.app.ActivityCompat.startActivityForResult; | ||
|
||
|
||
public class RapidPro extends Application { | ||
|
||
public static Logger LOG = new Logger(); | ||
|
@@ -76,6 +88,8 @@ public class RapidPro extends Application { | |
public static long MESSAGE_THROTTLE_WINDOW = 1000 * 60 * (MESSAGE_THROTTLE_MINUTES + 2); | ||
public static final long MESSAGE_RATE_LIMITER = 1000; | ||
|
||
public static final String MESSAGE_PACK_VERSION_SUPPORTED = "1.1"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since every time the message packs version change the app will have to change too, I hard coded the packs version so we pass that to the server to get that proper packs version |
||
|
||
public static final String PREF_LAST_UPDATE = "lastUpdate"; | ||
|
||
private SMSModem m_modem; | ||
|
@@ -457,8 +471,88 @@ public static void broadcastUpdatedCounts(Context context){ | |
context.sendBroadcast(intent); | ||
} | ||
|
||
private class DownloadPackFile extends AsyncTask<Integer, Integer, Long> { | ||
|
||
@Override | ||
protected Long doInBackground(final Integer... packs) { | ||
String packToInstall = packs[0].toString(); | ||
|
||
String endpoint = RapidPro.get().getServerURL(getApplicationContext()); | ||
String url = endpoint + "/android/?v=" + MESSAGE_PACK_VERSION_SUPPORTED + "&pack=" + packToInstall; | ||
|
||
String fileName = "Pack" + packToInstall + ".apk"; | ||
final File file = new File(getExternalFilesDir("Download").getAbsolutePath(), fileName); | ||
|
||
if(file.exists()) { | ||
file.delete(); | ||
} | ||
|
||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); | ||
request.setDescription(fileName); | ||
request.setTitle(fileName); | ||
request.setDestinationUri(Uri.fromFile(file)); | ||
request.setVisibleInDownloadsUi(false); | ||
|
||
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); | ||
final Long enqueuedId = manager.enqueue(request); | ||
|
||
final DownloadManager.Query managerQuery = new DownloadManager.Query(); | ||
managerQuery.setFilterById(enqueuedId); | ||
|
||
BroadcastReceiver receiver = new BroadcastReceiver() { | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
String action = intent.getAction(); | ||
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { | ||
|
||
Cursor cursor = manager.query(managerQuery); | ||
if (cursor.moveToFirst()) { | ||
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS); | ||
if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(columnIndex)) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
Uri apkUri = FileProvider.getUriForFile(getApplicationContext(), "io.rapidpro.androidchannel.provider", file); | ||
Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE); | ||
installIntent.setDataAndType(apkUri, "application/vnd.android.package-archive"); | ||
installIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); | ||
installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | ||
startActivity(installIntent); | ||
|
||
} else { | ||
Intent installIntent = new Intent(Intent.ACTION_VIEW); | ||
installIntent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); | ||
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
startActivity(installIntent); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); | ||
|
||
return enqueuedId; | ||
} | ||
|
||
} | ||
|
||
public String getServerURL(Context context) { | ||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); | ||
String endpoint = prefs.getString(SettingsActivity.SERVER, SyncHelper.ENDPOINT); | ||
String ip = prefs.getString(SettingsActivity.IP_ADDRESS, null); | ||
|
||
// if our endpoint is an ip, add :8000 to it | ||
if (endpoint.startsWith("ip")) { | ||
endpoint = "http://" + ip; | ||
if (!ip.contains(":")) { | ||
endpoint += ":8000"; | ||
} | ||
} | ||
|
||
return endpoint; | ||
|
||
} | ||
|
||
public void installPack(Context context){ | ||
public void installPack(final Context context){ | ||
List<String> packs = getInstalledPacks(); | ||
|
||
int packToInstall = 0; | ||
|
@@ -470,9 +564,7 @@ public void installPack(Context context){ | |
} | ||
|
||
if (packToInstall > 0) { | ||
Intent intent = new Intent(Intent.ACTION_VIEW); | ||
intent.setData(Uri.parse("market://details?id=io.rapidpro.androidchannel.pack" + packToInstall)); | ||
context.startActivity(intent); | ||
new DownloadPackFile().execute(packToInstall); | ||
} | ||
} | ||
|
||
|
@@ -490,6 +582,34 @@ public String getUUID(){ | |
return uuid; | ||
} | ||
|
||
public String refreshAppVersion(){ | ||
final PackageManager pm = getPackageManager(); | ||
PackageInfo pinfo = null; | ||
String appVersion = null; | ||
try { | ||
pinfo = pm.getPackageInfo(getPackageName(), 0); | ||
appVersion = pinfo.versionName; | ||
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit(); | ||
editor.putString(SettingsActivity.APP_VERSION, appVersion); | ||
editor.commit(); | ||
|
||
} catch (PackageManager.NameNotFoundException e) { | ||
RapidPro.LOG.e("Error getting package version name.", e); | ||
} | ||
return appVersion; | ||
} | ||
|
||
public String getAppVersion() { | ||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | ||
String appVersion = prefs.getString(SettingsActivity.APP_VERSION, null); | ||
|
||
if (appVersion == null){ | ||
appVersion = refreshAppVersion(); | ||
} | ||
|
||
return appVersion; | ||
} | ||
|
||
public void printDebug() { | ||
// some debug metrics | ||
int allotted = ((RapidPro)getApplicationContext()).getInstalledPacks().size() * RapidPro.MESSAGE_THROTTLE; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated but AFAIK is preferable to have these in app/build.gradle now