1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-27 10:07:32 +01:00

move installer progressbar from PebbleIoThread to GB and use that for Mi Band fw installation

This commit is contained in:
Andreas Shimokawa 2015-07-28 18:44:54 +02:00
parent 9f591ef8b5
commit 732f26823b
3 changed files with 45 additions and 41 deletions

View File

@ -266,4 +266,36 @@ public class GB {
.build(); .build();
return df.format(duration, unit); return df.format(duration, unit);
} }
private static Notification createInstallNotification(String text, boolean ongoing,
int percentage, Context context) {
Intent notificationIntent = new Intent(context, AppManagerActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(text)
.setTicker(text)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingIntent)
.setOngoing(ongoing);
if (ongoing) {
nb.setProgress(100, percentage, percentage == 0);
}
return nb.build();
}
public static void updateInstallNotification(String text, boolean ongoing, int percentage, Context context) {
Notification notification = createInstallNotification(text, ongoing, percentage, context);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, notification);
}
} }

View File

@ -900,10 +900,12 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
if ((i > 0) && (i % 50 == 0)) { if ((i > 0) && (i % 50 == 0)) {
if (!sendFirmwareSync()) { if (!sendFirmwareSync()) {
GB.updateInstallNotification("Firmware sync failed", false, 0, getContext());
LOG.error("Firmware sync failed"); LOG.error("Firmware sync failed");
return; return;
} }
} }
GB.updateInstallNotification("Firmware update in progress", true, (firmwareProgress / len) * 100, getContext());
LOG.info("Firmware update progress:" + firmwareProgress + " total lenL:" + len + " progress:" + firmwareProgress / len); LOG.info("Firmware update progress:" + firmwareProgress + " total lenL:" + len + " progress:" + firmwareProgress / len);
} }
@ -918,10 +920,14 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
} }
LOG.info("Firmware update progress:" + firmwareProgress + " total lenL:" + len + " progress:" + firmwareProgress / len); LOG.info("Firmware update progress:" + firmwareProgress + " total lenL:" + len + " progress:" + firmwareProgress / len);
if (firmwareProgress >= len) {
GB.updateInstallNotification("Firmware installation complete", false, 100, getContext());
} else {
GB.updateInstallNotification("Firmware update in progress", true, (firmwareProgress / len) * 100, getContext());
}
if (!sendFirmwareSync()) { if (!sendFirmwareSync()) {
LOG.error("Firmware sync failed"); LOG.error("Firmware sync failed");
return;
} }
} }
@ -933,6 +939,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
builder.queue(getQueue()); builder.queue(getQueue());
} catch (IOException ex) { } catch (IOException ex) {
LOG.error("Unable to send fw packet to MI", ex); LOG.error("Unable to send fw packet to MI", ex);
GB.updateInstallNotification("Firmware chunk write failed", false, 0, getContext());
return false; return false;
} }
return true; return true;
@ -945,6 +952,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
builder.queue(getQueue()); builder.queue(getQueue());
} catch (IOException ex) { } catch (IOException ex) {
LOG.error("Unable to send firmware sync to MI", ex); LOG.error("Unable to send firmware sync to MI", ex);
GB.updateInstallNotification("Firmwar sync failed", false, 0, getContext());
return false; return false;
} }
return true; return true;

View File

@ -1,18 +1,13 @@
package nodomain.freeyourgadget.gadgetbridge.pebble; package nodomain.freeyourgadget.gadgetbridge.pebble;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket; import android.bluetooth.BluetoothSocket;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.ParcelUuid; import android.os.ParcelUuid;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -24,7 +19,7 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import nodomain.freeyourgadget.gadgetbridge.AppManagerActivity; import nodomain.freeyourgadget.gadgetbridge.GB;
import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.GBDeviceIoThread; import nodomain.freeyourgadget.gadgetbridge.GBDeviceIoThread;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
@ -35,7 +30,6 @@ import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol;
public class PebbleIoThread extends GBDeviceIoThread { public class PebbleIoThread extends GBDeviceIoThread {
private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class); private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class);
private static final int NOTIFICATION_ID = 2;
private final PebbleProtocol mPebbleProtocol; private final PebbleProtocol mPebbleProtocol;
private final PebbleSupport mPebbleSupport; private final PebbleSupport mPebbleSupport;
private BluetoothAdapter mBtAdapter = null; private BluetoothAdapter mBtAdapter = null;
@ -66,36 +60,6 @@ public class PebbleIoThread extends GBDeviceIoThread {
mPebbleSupport = pebbleSupport; mPebbleSupport = pebbleSupport;
} }
public static Notification createInstallNotification(String text, boolean ongoing,
int percentage, Context context) {
Intent notificationIntent = new Intent(context, AppManagerActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(text)
.setTicker(text)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingIntent)
.setOngoing(ongoing);
if (ongoing) {
nb.setProgress(100, percentage, percentage == 0);
}
return nb.build();
}
public static void updateInstallNotification(String text, boolean ongoing, int percentage, Context context) {
Notification notification = createInstallNotification(text, ongoing, percentage, context);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, notification);
}
@Override @Override
protected boolean connect(String btDeviceAddress) { protected boolean connect(String btDeviceAddress) {
@ -175,7 +139,7 @@ public class PebbleIoThread extends GBDeviceIoThread {
} while (bytes < 2000); } while (bytes < 2000);
if (bytes > 0) { if (bytes > 0) {
updateInstallNotification(getContext().getString( GB.updateInstallNotification(getContext().getString(
R.string.installing_binary_d_d, (mCurrentInstallableIndex + 1), mPebbleInstallables.length), true, (int) (((float) mBytesWritten / mBinarySize) * 100), getContext()); R.string.installing_binary_d_d, (mCurrentInstallableIndex + 1), mPebbleInstallables.length), true, (int) (((float) mBytesWritten / mBinarySize) * 100), getContext());
writeInstallApp(mPebbleProtocol.encodeUploadChunk(mAppInstallToken, buffer, bytes)); writeInstallApp(mPebbleProtocol.encodeUploadChunk(mAppInstallToken, buffer, bytes));
mBytesWritten += bytes; mBytesWritten += bytes;
@ -441,9 +405,9 @@ public class PebbleIoThread extends GBDeviceIoThread {
return; return;
} }
if (hadError) { if (hadError) {
updateInstallNotification(getContext().getString(R.string.installation_failed_), false, 0, getContext()); GB.updateInstallNotification(getContext().getString(R.string.installation_failed_), false, 0, getContext());
} else { } else {
updateInstallNotification(getContext().getString(R.string.installation_successful), false, 0, getContext()); GB.updateInstallNotification(getContext().getString(R.string.installation_successful), false, 0, getContext());
} }
mInstallState = PebbleAppInstallState.UNKNOWN; mInstallState = PebbleAppInstallState.UNKNOWN;