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