mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-25 10:05:49 +01:00
Cleanup, deduplication, null-handling fixes
This commit is contained in:
parent
69be089c30
commit
e159a5dde3
@ -34,6 +34,7 @@ import android.widget.Toast;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
@ -58,13 +59,16 @@ public class ExternalPebbleJSActivity extends AbstractGBActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
GBDevice currentDevice;
|
||||
UUID currentUUID;
|
||||
GBDevice currentDevice = null;
|
||||
UUID currentUUID = null;
|
||||
Bundle extras = getIntent().getExtras();
|
||||
if (extras != null) {
|
||||
currentDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
|
||||
currentUUID = (UUID) extras.getSerializable(DeviceService.EXTRA_APP_UUID);
|
||||
|
||||
Objects.requireNonNull(currentDevice, "Must provide a device when invoking this activity");
|
||||
Objects.requireNonNull(currentUUID, "Must provide a uuid when invoking this activity");
|
||||
|
||||
if (GBApplication.getPrefs().getBoolean("pebble_enable_background_javascript", false)) {
|
||||
if (extras.getBoolean(SHOW_CONFIG, false)) {
|
||||
WebViewSingleton.runJavascriptInterface(currentDevice, currentUUID);
|
||||
@ -75,10 +79,9 @@ public class ExternalPebbleJSActivity extends AbstractGBActivity {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Must provide a device when invoking this activity");
|
||||
throw new IllegalArgumentException("Must provide device and uuid in extras when invoking this activity");
|
||||
}
|
||||
|
||||
|
||||
if (GBApplication.getPrefs().getBoolean("pebble_enable_background_javascript", false)) {
|
||||
setContentView(R.layout.activity_external_pebble_js);
|
||||
myWebView = WebViewSingleton.getWebView(this);
|
||||
|
@ -147,7 +147,7 @@ public abstract class AbstractAppManagerFragment extends Fragment {
|
||||
List<GBDeviceApp> cachedAppList = new ArrayList<>();
|
||||
File cachePath;
|
||||
try {
|
||||
cachePath = new File(FileUtils.getExternalFilesDir().getPath() + "/pbw-cache");
|
||||
cachePath = PebbleUtils.getPbwCacheDir();
|
||||
} catch (IOException e) {
|
||||
LOG.warn("could not get external dir while reading pbw cache.");
|
||||
return cachedAppList;
|
||||
@ -362,18 +362,18 @@ public abstract class AbstractAppManagerFragment extends Fragment {
|
||||
private boolean onContextItemSelected(MenuItem item, GBDeviceApp selectedApp) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.appmanager_app_delete_cache:
|
||||
String baseName;
|
||||
File pbwCacheDir;
|
||||
try {
|
||||
baseName = FileUtils.getExternalFilesDir().getPath() + "/pbw-cache/" + selectedApp.getUUID();
|
||||
pbwCacheDir = PebbleUtils.getPbwCacheDir();
|
||||
} catch (IOException e) {
|
||||
LOG.warn("could not get external dir while trying to access pbw cache.");
|
||||
return true;
|
||||
}
|
||||
|
||||
String baseName = selectedApp.getUUID().toString();
|
||||
String[] suffixToDelete = new String[]{".pbw", ".json", "_config.js", "_preset.json"};
|
||||
|
||||
for (String suffix : suffixToDelete) {
|
||||
File fileToDelete = new File(baseName + suffix);
|
||||
File fileToDelete = new File(pbwCacheDir,baseName + suffix);
|
||||
if (!fileToDelete.delete()) {
|
||||
LOG.warn("could not delete file from pbw cache: " + fileToDelete.toString());
|
||||
} else {
|
||||
@ -394,7 +394,7 @@ public abstract class AbstractAppManagerFragment extends Fragment {
|
||||
case R.id.appmanager_app_reinstall:
|
||||
File cachePath;
|
||||
try {
|
||||
cachePath = new File(FileUtils.getExternalFilesDir().getPath() + "/pbw-cache/" + selectedApp.getUUID() + ".pbw");
|
||||
cachePath = new File(PebbleUtils.getPbwCacheDir(), selectedApp.getUUID() + ".pbw");
|
||||
} catch (IOException e) {
|
||||
LOG.warn("could not get external dir while trying to access pbw cache.");
|
||||
return true;
|
||||
|
@ -154,7 +154,7 @@ public class PBWInstallHandler implements InstallHandler {
|
||||
File destDir;
|
||||
GBDeviceApp app = mPBWReader.getGBDeviceApp();
|
||||
try {
|
||||
destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
|
||||
destDir = PebbleUtils.getPbwCacheDir();
|
||||
destDir.mkdirs();
|
||||
FileUtils.copyURItoFile(mContext, mUri, new File(destDir, app.getUUID().toString() + ".pbw"));
|
||||
|
||||
|
@ -34,6 +34,7 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils;
|
||||
|
||||
class AppMessageHandler {
|
||||
final PebbleProtocol mPebbleProtocol;
|
||||
@ -73,7 +74,7 @@ class AppMessageHandler {
|
||||
}
|
||||
|
||||
JSONObject getAppKeys() throws IOException, JSONException {
|
||||
File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
|
||||
File destDir = PebbleUtils.getPbwCacheDir();
|
||||
File configurationFile = new File(destDir, mUUID.toString() + ".json");
|
||||
if (configurationFile.exists()) {
|
||||
String jsonstring = FileUtils.getStringFromFile(configurationFile);
|
||||
|
@ -505,7 +505,7 @@ class PebbleIoThread extends GBDeviceIoThread {
|
||||
case REQUEST:
|
||||
LOG.info("APPFETCH request: " + appMgmt.uuid + " / " + appMgmt.token);
|
||||
try {
|
||||
installApp(Uri.fromFile(new File(FileUtils.getExternalFilesDir() + "/pbw-cache/" + appMgmt.uuid.toString() + ".pbw")), appMgmt.token);
|
||||
installApp(Uri.fromFile(new File(PebbleUtils.getPbwCacheDir(),appMgmt.uuid.toString() + ".pbw")), appMgmt.token);
|
||||
} catch (IOException e) {
|
||||
LOG.error("Error installing app: " + e.getMessage(), e);
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.webkit.JavascriptInterface;
|
||||
import android.widget.Toast;
|
||||
|
||||
@ -51,7 +52,7 @@ public class JSInterface {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JSInterface.class);
|
||||
|
||||
public JSInterface(GBDevice device, UUID mUuid) {
|
||||
public JSInterface(@NonNull GBDevice device, @NonNull UUID mUuid) {
|
||||
LOG.debug("Creating JS interface for UUID: " + mUuid.toString());
|
||||
this.device = device;
|
||||
this.mUuid = mUuid;
|
||||
@ -73,6 +74,10 @@ public class JSInterface {
|
||||
boolean needsTransaction = "true".equals(needsTransactionMsg);
|
||||
LOG.debug("from WEBVIEW: " + msg + " needs a transaction: " + needsTransaction);
|
||||
JSONObject knownKeys = WebViewSingleton.getAppConfigurationKeys(this.mUuid);
|
||||
if (knownKeys == null) {
|
||||
LOG.warn("No app configuration keys for: " + mUuid);
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
JSONObject in = new JSONObject(msg);
|
||||
@ -139,7 +144,7 @@ public class JSInterface {
|
||||
public String getAppConfigurationFile() {
|
||||
LOG.debug("WEBVIEW loading config file of " + this.mUuid.toString());
|
||||
try {
|
||||
File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
|
||||
File destDir = PebbleUtils.getPbwCacheDir();
|
||||
File configurationFile = new File(destDir, this.mUuid.toString() + "_config.js");
|
||||
if (configurationFile.exists()) {
|
||||
return "file:///" + configurationFile.getAbsolutePath();
|
||||
@ -153,7 +158,7 @@ public class JSInterface {
|
||||
@JavascriptInterface
|
||||
public String getAppStoredPreset() {
|
||||
try {
|
||||
File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
|
||||
File destDir = PebbleUtils.getPbwCacheDir();
|
||||
File configurationFile = new File(destDir, this.mUuid.toString() + "_preset.json");
|
||||
if (configurationFile.exists()) {
|
||||
return FileUtils.getStringFromFile(configurationFile);
|
||||
@ -170,7 +175,7 @@ public class JSInterface {
|
||||
Writer writer;
|
||||
|
||||
try {
|
||||
File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
|
||||
File destDir = PebbleUtils.getPbwCacheDir();
|
||||
File presetsFile = new File(destDir, this.mUuid.toString() + "_preset.json");
|
||||
writer = new BufferedWriter(new FileWriter(presetsFile));
|
||||
writer.write(msg);
|
||||
|
@ -18,6 +18,9 @@ package nodomain.freeyourgadget.gadgetbridge.util;
|
||||
|
||||
import android.graphics.Color;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class PebbleUtils {
|
||||
public static String getPlatformName(String hwRev) {
|
||||
String platformName;
|
||||
@ -90,4 +93,13 @@ public class PebbleUtils {
|
||||
public static byte getPebbleColor(String colorHex) {
|
||||
return getPebbleColor(Color.parseColor(colorHex));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the directory containing the .pbw cache.
|
||||
* @throws IOException when the external files directory cannot be accessed
|
||||
*/
|
||||
public static File getPbwCacheDir() throws IOException {
|
||||
return new File(FileUtils.getExternalFilesDir(), "pbw-cache");
|
||||
}
|
||||
}
|
||||
|
@ -149,10 +149,7 @@ public class WebViewSingleton {
|
||||
return webViewSingleton.instance;
|
||||
}
|
||||
|
||||
public static void runJavascriptInterface(GBDevice device, UUID uuid) {
|
||||
if (uuid == null && device == null) {
|
||||
throw new RuntimeException("Javascript interface started without device and uuid");
|
||||
}
|
||||
public static void runJavascriptInterface(@NonNull GBDevice device, @NonNull UUID uuid) {
|
||||
if (uuid.equals(currentRunningUUID)) {
|
||||
LOG.debug("WEBVIEW uuid not changed keeping the old context");
|
||||
} else {
|
||||
@ -256,7 +253,7 @@ public class WebViewSingleton {
|
||||
|
||||
public static JSONObject getAppConfigurationKeys(UUID uuid) {
|
||||
try {
|
||||
File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache");
|
||||
File destDir = PebbleUtils.getPbwCacheDir();
|
||||
File configurationFile = new File(destDir, uuid.toString() + ".json");
|
||||
if (configurationFile.exists()) {
|
||||
String jsonString = FileUtils.getStringFromFile(configurationFile);
|
||||
|
Loading…
Reference in New Issue
Block a user