Skip to content

Commit

Permalink
Merge pull request #19 from rapidpro/App-version
Browse files Browse the repository at this point in the history
App version
  • Loading branch information
nicpottier authored Mar 19, 2019
2 parents bd31615 + 0e744e5 commit 16c3618
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 23 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ android {
applicationId "io.rapidpro.androidchannel"
minSdkVersion 21
targetSdkVersion 26
versionCode 198
versionName "1.9.8"
}

buildTypes {
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.rapidpro.androidchannel"
android:versionCode="197"
android:versionName="1.9.7" >
xmlns:tools="http://schemas.android.com/tools"
package="io.rapidpro.androidchannel" >

<!-- App receives FCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
Expand All @@ -14,6 +13,8 @@
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down Expand Up @@ -140,6 +141,16 @@
android:exported="true"
android:name=".contentprovider.DBCommandContentProvider"
android:authorities="io.rapidpro.androidchannel.commands" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="io.rapidpro.androidchannel.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>


</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class HomeActivity extends BaseActivity implements Intents {
Manifest.permission.SEND_SMS,
Manifest.permission.READ_SMS,
Manifest.permission.INTERNET,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WAKE_LOCK,
Manifest.permission.ACCESS_NETWORK_STATE,
Expand Down Expand Up @@ -82,6 +83,8 @@ public class HomeActivity extends BaseActivity implements Intents {
// private TextView m_secret;
private TextView m_status;

private TextView m_appVersion;

private static HomeActivity s_this;
private DashboardReceiver m_receiver;

Expand All @@ -106,6 +109,8 @@ public void onCreate(Bundle savedInstanceState) {
m_lastUpdated = findViewById(R.id.last_updated);
m_lastUpdate = findViewById(R.id.last_update);

RapidPro.get().refreshAppVersion();

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());

Boolean showAdvancedSettings = prefs.getBoolean(SHOW_ADVANCED_SETTINGS, false);
Expand Down Expand Up @@ -170,6 +175,9 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
m_status = (TextView)findViewById(R.id.status);
m_statusBar = (LinearLayout)findViewById(R.id.status_bar);

m_appVersion = (TextView)findViewById(R.id.appversion);
m_appVersion.setText("v" + RapidPro.get().getAppVersion());

s_this = this;

m_outgoingCount = (TextView) findViewById(R.id.outgoing_count);
Expand Down Expand Up @@ -216,6 +224,7 @@ public void onResume() {
filter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(m_receiver, filter);

RapidPro.get().refreshInstalledPacks();
RapidPro.broadcastUpdatedCounts(this);

if (hasAcceptedPermissions()) {
Expand Down
128 changes: 124 additions & 4 deletions app/src/main/java/io/rapidpro/androidchannel/RapidPro.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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";

public static final String PREF_LAST_UPDATE = "lastUpdate";

private SMSModem m_modem;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
public static final String LAST_AIRPLANE_TOGGLE = "last_airplane_toggle";
public static final String UUID = "uuid";

public static final String APP_VERSION = "appVersion";

public static final String RESET = "reset";
public static final String AIRPLANE_RESET = "airplane_reset";

Expand Down
10 changes: 1 addition & 9 deletions app/src/main/java/io/rapidpro/androidchannel/SyncHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ public void sync() {

String relayerId = prefs.getString(SettingsActivity.RELAYER_ID, null);
String secret = prefs.getString(SettingsActivity.RELAYER_SECRET, null);
String endpoint = prefs.getString(SettingsActivity.SERVER, ENDPOINT);
String ip = prefs.getString(SettingsActivity.IP_ADDRESS, null);


long lastAirplane = prefs.getLong(SettingsActivity.LAST_AIRPLANE_TOGGLE, -1l);
Expand All @@ -124,13 +122,7 @@ public void sync() {
return;
}

// if our endpoint is an ip, add :8000 to it
if (endpoint.startsWith("ip")) {
endpoint = "http://" + ip;
if (!ip.contains(":")) {
endpoint += ":8000";
}
}
String endpoint = RapidPro.get().getServerURL(context);

// flip to whatever network is preferred by our user
setNetworkType(network);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.BatteryManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import io.rapidpro.androidchannel.SettingsActivity;
import io.rapidpro.androidchannel.RapidPro;
import io.rapidpro.androidchannel.data.DBCommandHelper;
import io.rapidpro.androidchannel.json.JSON;

import java.util.ArrayList;
import java.util.List;

import io.rapidpro.androidchannel.RapidPro;
import io.rapidpro.androidchannel.SettingsActivity;
import io.rapidpro.androidchannel.data.DBCommandHelper;
import io.rapidpro.androidchannel.json.JSON;

public class StatusCommand extends Command {
public static final String CMD = "status";

Expand All @@ -51,6 +54,7 @@ public class StatusCommand extends Command {

private String m_device;
private String m_os;
private String m_appVersion;

public static final String POWER_SOURCE = "p_src";
public static final String POWER_LEVEL = "p_lvl";
Expand Down Expand Up @@ -79,6 +83,7 @@ public class StatusCommand extends Command {

public static final String DEVICE = "dev";
public static final String OS = "os";
public static final String APP_VERSION = "app_version";

public StatusCommand(Context context) {
super(CMD);
Expand Down Expand Up @@ -150,6 +155,9 @@ public StatusCommand(Context context) {
m_relayerOrg = preferences.getInt(SettingsActivity.RELAYER_ORG, -1);
m_device = Build.MODEL;
m_os = Build.VERSION.RELEASE;

m_appVersion = RapidPro.get().getAppVersion();

}

@Override
Expand All @@ -173,6 +181,7 @@ public JSON asJSON() {

json.put(DEVICE, m_device);
json.put(OS, m_os);
json.put(APP_VERSION, m_appVersion);

return json;
}
Expand Down
Loading

0 comments on commit 16c3618

Please sign in to comment.