Search in sources :

Example 11 with GBDeviceApp

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp in project Gadgetbridge by Freeyourgadget.

the class PBWInstallHandler method validateInstallation.

@Override
public void validateInstallation(InstallActivity installActivity, GBDevice device) {
    if (device.isBusy()) {
        installActivity.setInfoText(device.getBusyTask());
        installActivity.setInstallEnabled(false);
        return;
    }
    if (device.getType() != DeviceType.PEBBLE || !device.isConnected()) {
        installActivity.setInfoText("Element cannot be installed");
        installActivity.setInstallEnabled(false);
        return;
    }
    String platformName = PebbleUtils.getPlatformName(device.getModel());
    try {
        mPBWReader = new PBWReader(mUri, mContext, platformName);
    } catch (FileNotFoundException e) {
        installActivity.setInfoText("file not found");
        installActivity.setInstallEnabled(false);
        return;
    } catch (IOException e) {
        installActivity.setInfoText("error reading file");
        installActivity.setInstallEnabled(false);
        return;
    }
    if (!mPBWReader.isValid()) {
        installActivity.setInfoText("pbw/pbz is broken or incompatible with your Hardware or Firmware.");
        installActivity.setInstallEnabled(false);
        return;
    }
    GenericItem installItem = new GenericItem();
    // FIXME: do not set twice
    installItem.setIcon(R.drawable.ic_watchapp);
    if (mPBWReader.isFirmware()) {
        installItem.setIcon(R.drawable.ic_firmware);
        String hwRevision = mPBWReader.getHWRevision();
        if (hwRevision != null && hwRevision.equals(device.getModel())) {
            installItem.setName(mContext.getString(R.string.pbw_installhandler_pebble_firmware, ""));
            installItem.setDetails(mContext.getString(R.string.pbwinstallhandler_correct_hw_revision));
            installActivity.setInfoText(mContext.getString(R.string.firmware_install_warning, hwRevision));
            installActivity.setInstallEnabled(true);
        } else {
            if (hwRevision != null) {
                installItem.setName(mContext.getString(R.string.pbw_installhandler_pebble_firmware, hwRevision));
                installItem.setDetails(mContext.getString(R.string.pbwinstallhandler_incorrect_hw_revision));
            }
            installActivity.setInfoText(mContext.getString(R.string.pbw_install_handler_hw_revision_mismatch));
            installActivity.setInstallEnabled(false);
        }
    } else {
        GBDeviceApp app = mPBWReader.getGBDeviceApp();
        if (app != null) {
            installItem.setName(app.getName());
            installItem.setDetails(mContext.getString(R.string.pbwinstallhandler_app_item, app.getCreator(), app.getVersion()));
            int drawable;
            if (mPBWReader.isLanguage()) {
                drawable = R.drawable.ic_languagepack;
            } else {
                switch(app.getType()) {
                    case WATCHFACE:
                        drawable = R.drawable.ic_watchface;
                        break;
                    case APP_ACTIVITYTRACKER:
                        drawable = R.drawable.ic_activitytracker;
                        break;
                    default:
                        drawable = R.drawable.ic_watchapp;
                }
            }
            installItem.setIcon(drawable);
            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);
        }
    }
    if (installItem.getName() != null) {
        installActivity.setInstallItem(installItem);
    }
}
Also used : GenericItem(nodomain.freeyourgadget.gadgetbridge.model.GenericItem) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) GBDeviceApp(nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp)

Example 12 with GBDeviceApp

use of nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp in project Gadgetbridge by Freeyourgadget.

the class PBWInstallHandler method onStartInstall.

@Override
public void onStartInstall(GBDevice device) {
    if (mPBWReader.isFirmware() || mPBWReader.isLanguage()) {
        return;
    }
    File destDir;
    GBDeviceApp app = mPBWReader.getGBDeviceApp();
    try {
        destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
        destDir.mkdirs();
        FileUtils.copyURItoFile(mContext, mUri, new File(destDir, app.getUUID().toString() + ".pbw"));
        AppManagerActivity.addToAppOrderFile("pbwcacheorder.txt", app.getUUID());
    } catch (IOException e) {
        LOG.error("Installation failed: " + e.getMessage(), e);
        return;
    }
    File outputFile = new File(destDir, app.getUUID().toString() + ".json");
    Writer writer;
    try {
        writer = new BufferedWriter(new FileWriter(outputFile));
    } catch (IOException e) {
        LOG.error("Failed to open output file: " + e.getMessage(), e);
        return;
    }
    try {
        LOG.info(app.getJSON().toString());
        JSONObject appJSON = app.getJSON();
        JSONObject appKeysJSON = mPBWReader.getAppKeysJSON();
        if (appKeysJSON != null) {
            appJSON.put("appKeys", appKeysJSON);
        }
        writer.write(appJSON.toString());
        writer.close();
    } catch (IOException e) {
        LOG.error("Failed to write to output file: " + e.getMessage(), e);
    } catch (JSONException e) {
        LOG.error(e.getMessage(), e);
    }
    InputStream jsConfigFile = mPBWReader.getInputStreamFile("pebble-js-app.js");
    if (jsConfigFile != null) {
        try {
            outputFile = new File(destDir, app.getUUID().toString() + "_config.js");
            FileUtils.copyStreamToFile(jsConfigFile, outputFile);
        } catch (IOException e) {
            LOG.error("Failed to open output file: " + e.getMessage(), e);
        } finally {
            try {
                jsConfigFile.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : JSONObject(org.json.JSONObject) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) JSONException(org.json.JSONException) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) GBDeviceApp(nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp) BufferedWriter(java.io.BufferedWriter)

Aggregations

GBDeviceApp (nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp)12 ArrayList (java.util.ArrayList)6 UUID (java.util.UUID)5 IOException (java.io.IOException)3 File (java.io.File)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 PackageManager (android.content.pm.PackageManager)1 Pair (android.util.Pair)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 PopupMenu (android.widget.PopupMenu)1 BufferedWriter (java.io.BufferedWriter)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 ByteBuffer (java.nio.ByteBuffer)1 GBDeviceEvent (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent)1 GBDeviceEventAppInfo (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo)1