2015-08-03 23:09:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
2015-03-25 22:23:45 +01:00
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2015-09-10 23:07:42 +02:00
|
|
|
import android.content.SharedPreferences;
|
2016-01-02 12:24:23 +01:00
|
|
|
import android.net.Uri;
|
2015-03-25 22:23:45 +01:00
|
|
|
import android.os.Bundle;
|
2015-09-10 23:07:42 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2015-03-27 11:23:30 +01:00
|
|
|
import android.support.v4.app.NavUtils;
|
2015-03-27 10:56:08 +01:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2016-04-09 05:41:31 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2015-03-26 18:11:47 +01:00
|
|
|
import android.view.ContextMenu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
2015-03-25 22:23:45 +01:00
|
|
|
import android.widget.ListView;
|
|
|
|
|
2015-12-07 18:15:26 +01:00
|
|
|
import org.json.JSONObject;
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-03-25 22:23:45 +01:00
|
|
|
|
2015-08-11 13:55:35 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2015-05-18 20:56:19 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2015-03-25 22:23:45 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAppAdapter;
|
2016-03-03 11:52:30 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2015-08-11 13:55:35 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp;
|
2016-01-27 23:00:44 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol;
|
2015-08-11 13:55:35 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
2016-03-08 11:49:08 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils;
|
2015-03-25 22:23:45 +01:00
|
|
|
|
|
|
|
|
2016-04-09 05:41:31 +02:00
|
|
|
public class AppManagerActivity extends AppCompatActivity {
|
2015-03-25 22:23:45 +01:00
|
|
|
public static final String ACTION_REFRESH_APPLIST
|
2015-07-18 23:38:59 +02:00
|
|
|
= "nodomain.freeyourgadget.gadgetbridge.appmanager.action.refresh_applist";
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AppManagerActivity.class);
|
2015-03-27 10:56:08 +01:00
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
2015-03-25 22:23:45 +01:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
2015-12-07 23:33:32 +01:00
|
|
|
if (action.equals(GBApplication.ACTION_QUIT)) {
|
2015-03-25 22:23:45 +01:00
|
|
|
finish();
|
|
|
|
} else if (action.equals(ACTION_REFRESH_APPLIST)) {
|
2015-03-26 18:11:47 +01:00
|
|
|
appList.clear();
|
2015-03-25 22:23:45 +01:00
|
|
|
int appCount = intent.getIntExtra("app_count", 0);
|
|
|
|
for (Integer i = 0; i < appCount; i++) {
|
|
|
|
String appName = intent.getStringExtra("app_name" + i.toString());
|
|
|
|
String appCreator = intent.getStringExtra("app_creator" + i.toString());
|
2015-05-18 20:56:19 +02:00
|
|
|
UUID uuid = UUID.fromString(intent.getStringExtra("app_uuid" + i.toString()));
|
2015-03-31 23:34:19 +02:00
|
|
|
GBDeviceApp.Type appType = GBDeviceApp.Type.values()[intent.getIntExtra("app_type" + i.toString(), 0)];
|
2015-03-26 18:11:47 +01:00
|
|
|
|
2015-05-18 20:56:19 +02:00
|
|
|
appList.add(new GBDeviceApp(uuid, appName, appCreator, "", appType));
|
2015-03-25 22:23:45 +01:00
|
|
|
}
|
2015-09-10 23:07:42 +02:00
|
|
|
|
|
|
|
if (sharedPrefs.getBoolean("pebble_force_untested", false)) {
|
|
|
|
appList.addAll(getSystemApps());
|
|
|
|
}
|
|
|
|
|
2015-03-25 22:23:45 +01:00
|
|
|
mGBDeviceAppAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-09-10 23:07:42 +02:00
|
|
|
|
2015-09-12 00:19:36 +02:00
|
|
|
private SharedPreferences sharedPrefs;
|
2015-09-10 23:07:42 +02:00
|
|
|
|
|
|
|
private final List<GBDeviceApp> appList = new ArrayList<>();
|
2015-03-26 18:11:47 +01:00
|
|
|
private GBDeviceAppAdapter mGBDeviceAppAdapter;
|
|
|
|
private GBDeviceApp selectedApp = null;
|
2016-03-03 11:52:30 +01:00
|
|
|
private GBDevice mGBDevice = null;
|
2015-03-25 22:23:45 +01:00
|
|
|
|
2015-09-10 23:07:42 +02:00
|
|
|
private List<GBDeviceApp> getSystemApps() {
|
|
|
|
List<GBDeviceApp> systemApps = new ArrayList<>();
|
2015-12-12 11:59:52 +01:00
|
|
|
systemApps.add(new GBDeviceApp(UUID.fromString("4dab81a6-d2fc-458a-992c-7a1f3b96a970"), "Sports (System)", "Pebble Inc.", "", GBDeviceApp.Type.APP_SYSTEM));
|
|
|
|
systemApps.add(new GBDeviceApp(UUID.fromString("cf1e816a-9db0-4511-bbb8-f60c48ca8fac"), "Golf (System)", "Pebble Inc.", "", GBDeviceApp.Type.APP_SYSTEM));
|
2016-03-28 23:44:39 +02:00
|
|
|
if (mGBDevice != null && !"aplite".equals(PebbleUtils.getPlatformName(mGBDevice.getHardwareVersion()))) {
|
2016-03-08 11:49:08 +01:00
|
|
|
systemApps.add(new GBDeviceApp(PebbleProtocol.UUID_PEBBLE_HEALTH, "Health (System)", "Pebble Inc.", "", GBDeviceApp.Type.APP_SYSTEM));
|
|
|
|
}
|
2015-09-10 23:07:42 +02:00
|
|
|
|
|
|
|
return systemApps;
|
|
|
|
}
|
|
|
|
|
2015-08-11 13:55:35 +02:00
|
|
|
private List<GBDeviceApp> getCachedApps() {
|
|
|
|
List<GBDeviceApp> cachedAppList = new ArrayList<>();
|
2015-12-07 18:15:26 +01:00
|
|
|
File cachePath;
|
2015-08-11 13:55:35 +02:00
|
|
|
try {
|
2015-12-07 18:15:26 +01:00
|
|
|
cachePath = new File(FileUtils.getExternalFilesDir().getPath() + "/pbw-cache");
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("could not get external dir while reading pbw cache.");
|
|
|
|
return cachedAppList;
|
|
|
|
}
|
|
|
|
|
|
|
|
File files[] = cachePath.listFiles();
|
|
|
|
if (files != null) {
|
|
|
|
for (File file : files) {
|
|
|
|
if (file.getName().endsWith(".pbw")) {
|
|
|
|
String baseName = file.getName().substring(0, file.getName().length() - 4);
|
2016-03-18 16:47:14 +01:00
|
|
|
//metadata
|
2015-12-07 18:15:26 +01:00
|
|
|
File jsonFile = new File(cachePath, baseName + ".json");
|
2016-03-18 16:47:14 +01:00
|
|
|
//configuration
|
|
|
|
File configFile = new File(cachePath, baseName + "_config.js");
|
2015-12-07 18:15:26 +01:00
|
|
|
try {
|
|
|
|
String jsonstring = FileUtils.getStringFromFile(jsonFile);
|
|
|
|
JSONObject json = new JSONObject(jsonstring);
|
2016-03-18 16:47:14 +01:00
|
|
|
cachedAppList.add(new GBDeviceApp(json, configFile.exists()));
|
2015-12-07 18:15:26 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.warn("could not read json file for " + baseName, e.getMessage(), e);
|
|
|
|
cachedAppList.add(new GBDeviceApp(UUID.fromString(baseName), baseName, "N/A", "", GBDeviceApp.Type.UNKNOWN));
|
2015-08-13 10:06:32 +02:00
|
|
|
}
|
2015-08-11 13:55:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cachedAppList;
|
|
|
|
}
|
|
|
|
|
2015-03-25 22:23:45 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2015-09-10 23:07:42 +02:00
|
|
|
|
2016-03-03 11:52:30 +01:00
|
|
|
Bundle extras = getIntent().getExtras();
|
|
|
|
if (extras != null) {
|
|
|
|
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Must provide a device when invoking this activity");
|
|
|
|
}
|
|
|
|
|
2015-09-10 23:07:42 +02:00
|
|
|
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
|
|
|
|
2015-03-25 22:23:45 +01:00
|
|
|
setContentView(R.layout.activity_appmanager);
|
|
|
|
|
2015-05-12 20:09:35 +02:00
|
|
|
ListView appListView = (ListView) findViewById(R.id.appListView);
|
2015-03-25 22:23:45 +01:00
|
|
|
mGBDeviceAppAdapter = new GBDeviceAppAdapter(this, appList);
|
|
|
|
appListView.setAdapter(this.mGBDeviceAppAdapter);
|
2015-05-18 22:20:01 +02:00
|
|
|
|
|
|
|
appListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView parent, View v, int position, long id) {
|
2015-05-21 18:17:39 +02:00
|
|
|
UUID uuid = appList.get(position).getUUID();
|
2015-09-13 21:44:26 +02:00
|
|
|
GBApplication.deviceService().onAppStart(uuid, true);
|
2015-05-18 22:20:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-26 18:11:47 +01:00
|
|
|
registerForContextMenu(appListView);
|
2015-08-13 10:06:32 +02:00
|
|
|
|
2015-09-10 23:07:42 +02:00
|
|
|
appList.addAll(getCachedApps());
|
|
|
|
|
|
|
|
if (sharedPrefs.getBoolean("pebble_force_untested", false)) {
|
|
|
|
appList.addAll(getSystemApps());
|
2015-08-11 14:16:08 +02:00
|
|
|
}
|
2015-03-25 22:23:45 +01:00
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
2015-12-07 23:33:32 +01:00
|
|
|
filter.addAction(GBApplication.ACTION_QUIT);
|
2015-03-25 22:23:45 +01:00
|
|
|
filter.addAction(ACTION_REFRESH_APPLIST);
|
2015-03-27 10:56:08 +01:00
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
|
2015-03-25 22:23:45 +01:00
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
GBApplication.deviceService().onAppInfoReq();
|
2015-03-25 22:23:45 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 18:11:47 +01:00
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
|
|
|
super.onCreateContextMenu(menu, v, menuInfo);
|
2016-01-02 12:24:23 +01:00
|
|
|
getMenuInflater().inflate(R.menu.appmanager_context, menu);
|
2015-03-26 18:11:47 +01:00
|
|
|
AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) menuInfo;
|
|
|
|
selectedApp = appList.get(acmi.position);
|
2016-01-02 12:24:23 +01:00
|
|
|
|
2016-02-06 21:23:21 +01:00
|
|
|
if (!selectedApp.isInCache()) {
|
2016-01-02 12:24:23 +01:00
|
|
|
menu.removeItem(R.id.appmanager_app_reinstall);
|
|
|
|
}
|
2016-02-06 21:23:21 +01:00
|
|
|
if (!PebbleProtocol.UUID_PEBBLE_HEALTH.equals(selectedApp.getUUID())) {
|
|
|
|
menu.removeItem(R.id.appmanager_health_activate);
|
|
|
|
menu.removeItem(R.id.appmanager_health_deactivate);
|
|
|
|
} else if (PebbleProtocol.UUID_PEBBLE_HEALTH.equals(selectedApp.getUUID())) {
|
|
|
|
menu.removeItem(R.id.appmanager_app_delete);
|
|
|
|
}
|
2016-03-18 16:47:14 +01:00
|
|
|
if (!selectedApp.isConfigurable()) {
|
|
|
|
menu.removeItem(R.id.appmanager_app_configure);
|
|
|
|
}
|
2015-03-26 18:11:47 +01:00
|
|
|
menu.setHeaderTitle(selectedApp.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onContextItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
2016-02-06 21:23:21 +01:00
|
|
|
case R.id.appmanager_health_deactivate:
|
2015-03-26 18:11:47 +01:00
|
|
|
case R.id.appmanager_app_delete:
|
2016-01-02 12:24:23 +01:00
|
|
|
GBApplication.deviceService().onAppDelete(selectedApp.getUUID());
|
|
|
|
return true;
|
|
|
|
case R.id.appmanager_app_reinstall:
|
|
|
|
File cachePath;
|
|
|
|
try {
|
|
|
|
cachePath = new File(FileUtils.getExternalFilesDir().getPath() + "/pbw-cache/" + selectedApp.getUUID() + ".pbw");
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("could not get external dir while reading pbw cache.");
|
|
|
|
return true;
|
2015-03-26 18:11:47 +01:00
|
|
|
}
|
2016-01-02 12:24:23 +01:00
|
|
|
GBApplication.deviceService().onInstallApp(Uri.fromFile(cachePath));
|
2015-03-26 18:11:47 +01:00
|
|
|
return true;
|
2016-02-06 21:23:21 +01:00
|
|
|
case R.id.appmanager_health_activate:
|
|
|
|
GBApplication.deviceService().onInstallApp(Uri.parse("fake://health"));
|
|
|
|
return true;
|
2016-02-28 22:25:21 +01:00
|
|
|
case R.id.appmanager_app_configure:
|
2016-03-03 14:25:44 +01:00
|
|
|
GBApplication.deviceService().onAppStart(selectedApp.getUUID(), true);
|
2016-03-03 15:47:00 +01:00
|
|
|
|
2016-02-28 22:25:21 +01:00
|
|
|
Intent startIntent = new Intent(getApplicationContext(), ExternalPebbleJSActivity.class);
|
|
|
|
startIntent.putExtra("app_uuid", selectedApp.getUUID());
|
2016-03-03 11:52:30 +01:00
|
|
|
startIntent.putExtra(GBDevice.EXTRA_DEVICE, mGBDevice);
|
2016-02-28 22:25:21 +01:00
|
|
|
startActivity(startIntent);
|
|
|
|
return true;
|
2015-03-26 18:11:47 +01:00
|
|
|
default:
|
|
|
|
return super.onContextItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 11:23:30 +01:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2015-03-25 22:23:45 +01:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2015-03-27 10:56:08 +01:00
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
2015-03-25 22:23:45 +01:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
}
|