2015-08-06 02:17:38 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-08-11 12:04:45 +02:00
|
|
|
import android.content.SharedPreferences;
|
2015-08-06 02:17:38 +02:00
|
|
|
import android.net.Uri;
|
2015-08-11 12:04:45 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2015-08-06 02:17:38 +02:00
|
|
|
|
2015-08-11 12:04:45 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-08-06 02:17:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.InstallActivity;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
2015-08-11 12:04:45 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
2015-08-06 02:17:38 +02:00
|
|
|
|
|
|
|
public class PBWInstallHandler implements InstallHandler {
|
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
private final PBWReader mPBWReader;
|
|
|
|
private final Uri mUri;
|
|
|
|
|
|
|
|
public PBWInstallHandler(Uri uri, Context context) {
|
|
|
|
mContext = context;
|
2015-08-11 12:47:23 +02:00
|
|
|
mPBWReader = new PBWReader(uri, context, ""); // FIXME: we should know the platform here
|
2015-08-06 02:17:38 +02:00
|
|
|
mUri = uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void validateInstallation(InstallActivity installActivity, GBDevice device) {
|
|
|
|
if (device.isBusy() || device.getType() != DeviceType.PEBBLE || !device.isConnected()) {
|
|
|
|
installActivity.setInfoText("Element cannot be installed");
|
|
|
|
installActivity.setInstallEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPBWReader.isFirmware()) {
|
|
|
|
String hwRevision = mPBWReader.getHWRevision();
|
|
|
|
if (hwRevision != null && hwRevision.equals(device.getHardwareVersion())) {
|
|
|
|
installActivity.setInfoText(mContext.getString(R.string.firmware_install_warning, hwRevision));
|
|
|
|
installActivity.setInstallEnabled(true);
|
|
|
|
} else {
|
|
|
|
installActivity.setInfoText(mContext.getString(R.string.pbw_install_handler_hw_revision_mismatch));
|
|
|
|
installActivity.setInstallEnabled(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GBDeviceApp app = mPBWReader.getGBDeviceApp();
|
|
|
|
if (app != null) {
|
|
|
|
installActivity.setInfoText(mContext.getString(R.string.app_install_info, app.getName(), app.getVersion(), app.getCreator()));
|
|
|
|
installActivity.setInstallEnabled(true);
|
|
|
|
} else {
|
|
|
|
installActivity.setInfoText(mContext.getString(R.string.pbw_install_handler_unable_to_install, mUri.getPath()));
|
|
|
|
installActivity.setInstallEnabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 12:04:45 +02:00
|
|
|
@Override
|
|
|
|
public void onStartInstall() {
|
|
|
|
if (mPBWReader.isFirmware()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext());
|
|
|
|
if (!sharedPrefs.getBoolean("pebble_force_untested", false)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GBDeviceApp app = mPBWReader.getGBDeviceApp();
|
2015-08-14 12:50:44 +02:00
|
|
|
File pbwFile = new File(mUri.getPath());
|
2015-08-11 12:04:45 +02:00
|
|
|
try {
|
|
|
|
File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
|
|
|
|
destDir.mkdirs();
|
|
|
|
FileUtils.copyFile(pbwFile, new File(destDir + "/" + app.getUUID().toString() + ".pbw"));
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 02:17:38 +02:00
|
|
|
public boolean isValid() {
|
|
|
|
return mPBWReader.isValid();
|
|
|
|
}
|
2015-08-11 12:04:45 +02:00
|
|
|
|
2015-08-06 02:17:38 +02:00
|
|
|
}
|