Pebble app installation: Only enable install button when device is connected, for firmware also check hardware revision

This commit is contained in:
Andreas Shimokawa 2015-04-20 20:49:14 +02:00
parent 412c771d59
commit 5487dfd348
2 changed files with 61 additions and 20 deletions

View File

@ -45,6 +45,7 @@ public class PBWReader {
private GBDeviceApp app;
private ArrayList<PebbleInstallable> pebbleInstallables;
private boolean isFirmware = false;
private String hwRevision = null;
public PBWReader(Uri uri, Context context) {
this.uri = uri;
@ -79,13 +80,13 @@ public class PBWReader {
String jsonString = baos.toString();
try {
JSONObject json = new JSONObject(jsonString);
String[] searchJSON;
HashMap<String, Byte> fileTypeMap;
try {
json.getJSONObject("firmware");
JSONObject firmware = json.getJSONObject("firmware");
fileTypeMap = fwFileTypesMap;
isFirmware = true;
hwRevision = firmware.getString("hwrev");
} catch (JSONException e) {
fileTypeMap = appFileTypesMap;
isFirmware = false;
@ -179,4 +180,7 @@ public class PBWReader {
return pebbleInstallables.toArray(new PebbleInstallable[pebbleInstallables.size()]);
}
public String getHWRevision() {
return hwRevision;
}
}

View File

@ -1,16 +1,22 @@
package nodomain.freeyourgadget.gadgetbridge.pebble;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.content.LocalBroadcastManager;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import nodomain.freeyourgadget.gadgetbridge.BluetoothCommunicationService;
import nodomain.freeyourgadget.gadgetbridge.ControlCenter;
import nodomain.freeyourgadget.gadgetbridge.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.GBDeviceApp;
import nodomain.freeyourgadget.gadgetbridge.R;
@ -22,6 +28,31 @@ public class PebbleAppInstallerActivity extends Activity {
TextView debugTextView;
Button installButton;
private PBWReader mPBWReader = null;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ControlCenter.ACTION_QUIT)) {
finish();
} else if (action.equals(GBDevice.ACTION_DEVICE_CHANGED)) {
GBDevice dev = intent.getParcelableExtra("device");
if (mPBWReader != null) {
if (mPBWReader.isFirmware()) {
String hwRevision = mPBWReader.getHWRevision();
if (hwRevision != null && hwRevision.equals(dev.getHardwareVersion()) && dev.isConnected()) {
installButton.setEnabled(true);
} else {
installButton.setEnabled(false);
}
} else {
installButton.setEnabled(dev.isConnected());
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -30,29 +61,34 @@ public class PebbleAppInstallerActivity extends Activity {
debugTextView = (TextView) findViewById(R.id.debugTextView);
installButton = (Button) findViewById(R.id.installButton);
IntentFilter filter = new IntentFilter();
filter.addAction(ControlCenter.ACTION_QUIT);
filter.addAction(GBDevice.ACTION_DEVICE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
final Uri uri = getIntent().getData();
PBWReader pbwReader = new PBWReader(uri, getApplicationContext());
GBDeviceApp app = pbwReader.getGBDeviceApp();
mPBWReader = new PBWReader(uri, getApplicationContext());
GBDeviceApp app = mPBWReader.getGBDeviceApp();
if (pbwReader != null) {
if (pbwReader.isFirmware()) {
debugTextView.setText("YOUR ARE TRYING TO INSTALL A FIRMWARE, PROCEED AT YOUR OWN RISK, MAKE SURE THIS FIRMWARE IS FOR YOUR PEBBLE REVISION, THERE ARE NO CHECKS.\n\n\n");
if (mPBWReader.isFirmware()) {
debugTextView.setText("YOUR ARE TRYING TO INSTALL A FIRMWARE, PROCEED AT YOUR OWN RISK, MAKE SURE THIS FIRMWARE IS FOR YOUR PEBBLE REVISION, THERE ARE NO CHECKS.\n\n\n");
} else if (app != null) {
debugTextView.setText("You are about to install the following app:\n\n\n" + app.getName() + " Version " + app.getVersion() + " by " + app.getCreator() + "\n");
}
installButton.setEnabled(true);
installButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(PebbleAppInstallerActivity.this, BluetoothCommunicationService.class);
startIntent.setAction(BluetoothCommunicationService.ACTION_INSTALL_PEBBLEAPP);
startIntent.putExtra("app_uri", uri.toString());
startService(startIntent);
}
});
} else if (app != null) {
debugTextView.setText("You are about to install the following app:\n\n\n" + app.getName() + " Version " + app.getVersion() + " by " + app.getCreator() + "\n");
}
installButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(PebbleAppInstallerActivity.this, BluetoothCommunicationService.class);
startIntent.setAction(BluetoothCommunicationService.ACTION_INSTALL_PEBBLEAPP);
startIntent.putExtra("app_uri", uri.toString());
startService(startIntent);
}
});
Intent versionInfoIntent = new Intent(this, BluetoothCommunicationService.class);
versionInfoIntent.setAction(BluetoothCommunicationService.ACTION_REQUEST_VERSIONINFO);
startService(versionInfoIntent);
}
@Override
@ -67,6 +103,7 @@ public class PebbleAppInstallerActivity extends Activity {
@Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
super.onDestroy();
}
}