1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +01:00

Introduce app shortcuts

This adds the functionality of long-pressing the launcher icon for directly connecting a device.
The devices are automatically added as shortcuts when they are being connected.
The helper library handles the maximum number of shortcuts automatically.
This commit is contained in:
Daniele Gobbetti 2022-10-01 17:08:15 +02:00 committed by Gitea
parent 5c6de5442b
commit b4b153c62d
3 changed files with 36 additions and 0 deletions

View File

@ -248,6 +248,7 @@ dependencies {
implementation 'com.github.wax911:android-emojify:0.1.7'
implementation 'com.google.protobuf:protobuf-javalite:3.10.0'
implementation 'com.android.volley:volley:1.2.1'
implementation 'androidx.core:core-google-shortcuts:1.0.1'
// JSR-310 timezones backport for Android, since we're still API 21
implementation 'com.jakewharton.threetenabp:threetenabp:1.4.0'

View File

@ -86,6 +86,8 @@ import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT;
//TODO: extend AbstractGBActivity, but it requires actionbar that is not available
public class ControlCenterv2 extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, GBActivity {
@ -319,6 +321,7 @@ public class ControlCenterv2 extends AppCompatActivity
@Override
protected void onResume() {
super.onResume();
handleShortcut(getIntent());
if (isLanguageInvalid) {
isLanguageInvalid = false;
recreate();
@ -549,6 +552,17 @@ public class ControlCenterv2 extends AppCompatActivity
return new RefreshTask(task, context);
}
private void handleShortcut(Intent intent) {
if(intent.getAction().equals(ACTION_CONNECT)) {
String btDeviceAddress = intent.getStringExtra("device");
if(btDeviceAddress!=null){
GBDevice candidate = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
if (candidate != null && !candidate.isConnected()) {
GBApplication.deviceService(candidate).connect();
}
}
}
}
public class RefreshTask extends DBAccess {
public RefreshTask(String task, Context context) {
super(task, context);

View File

@ -56,6 +56,9 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
import androidx.core.graphics.drawable.IconCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
@ -118,6 +121,8 @@ import nodomain.freeyourgadget.gadgetbridge.util.FormatUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT;
/**
* Adapter for displaying GBDevice instances.
*/
@ -281,6 +286,7 @@ public class GBDeviceAdapterv2 extends ListAdapter<GBDevice, GBDeviceAdapterv2.V
showTransientSnackbar(R.string.controlcenter_snackbar_need_longpress);
} else {
showTransientSnackbar(R.string.controlcenter_snackbar_connecting);
createDynamicShortcut(device);
GBApplication.deviceService(device).connect();
}
}
@ -1401,6 +1407,21 @@ public class GBDeviceAdapterv2 extends ListAdapter<GBDevice, GBDeviceAdapterv2.V
return Color.HSVToColor(hsvb);
}
void createDynamicShortcut(GBDevice device) {
Intent intent = new Intent(context, ControlCenterv2.class)
.setAction(ACTION_CONNECT)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
.putExtra("device", device.getAddress());
ShortcutManagerCompat.pushDynamicShortcut(context, new ShortcutInfoCompat.Builder(context, device.getAddress())
.setLongLived(false)
.setShortLabel(device.getAliasOrName())
.setIntent(intent)
.setIcon(IconCompat.createWithResource(context, device.getType().getIcon()))
.build()
);
}
private static class GBDeviceDiffUtil extends DiffUtil.ItemCallback<GBDevice> {
@Override
public boolean areItemsTheSame(@NonNull GBDevice oldItem, @NonNull GBDevice newItem) {