diff --git a/app/build.gradle b/app/build.gradle index 5de5a05bbd..bd0a3ddef9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -278,6 +278,8 @@ dependencies { def appauth_version = "0.7.1" def jcharset_version = "2.1" def apache_poi = "3.17" // Do not update + def reactivestreams_version = "1.0.3" + def rxjava2_version = "2.2.21" // https://developer.android.com/jetpack/androidx/releases/startup implementation "androidx.startup:startup-runtime:$startup_version" @@ -450,4 +452,9 @@ dependencies { // https://poi.apache.org/components/hmef/index.html // https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad implementation "org.apache.poi:poi-scratchpad:$apache_poi" + + // https://mvnrepository.com/artifact/org.reactivestreams/reactive-streams + // https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxjava + implementation "org.reactivestreams:reactive-streams:$reactivestreams_version" + implementation "io.reactivex.rxjava2:rxjava:$rxjava2_version" } diff --git a/app/src/fdroid/AndroidManifest.xml b/app/src/fdroid/AndroidManifest.xml index 51dfabcc15..302ca28008 100644 --- a/app/src/fdroid/AndroidManifest.xml +++ b/app/src/fdroid/AndroidManifest.xml @@ -395,6 +395,15 @@ + + + + + + + + + + + + + + + + + + createPublisherForAllAvailable() { + List controls = new ArrayList<>(); + Control control = new Control.StatelessBuilder(DEVICE_SYNC, getPendingIntent()) + .setCustomIcon(Icon.createWithResource(this, R.drawable.twotone_sync_24)) + .setTitle(getString(R.string.app_name)) + .setSubtitle(getString(R.string.title_widget_title_sync)) + .setDeviceType(DeviceTypes.TYPE_GENERIC_ON_OFF) + .build(); + controls.add(control); + return FlowAdapters.toFlowPublisher(Flowable.fromIterable(controls)); + } + + @NonNull + @Override + public Flow.Publisher createPublisherFor(@NonNull List controlIds) { + updatePublisher = ReplayProcessor.create(); + + if (controlIds.contains(DEVICE_SYNC)) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); + boolean enabled = prefs.getBoolean("enabled", true); + + Control control = new Control.StatefulBuilder(DEVICE_SYNC, getPendingIntent()) + .setCustomIcon(Icon.createWithResource(this, enabled + ? R.drawable.twotone_sync_24 + : R.drawable.twotone_sync_disabled_24)) + .setTitle(getString(R.string.app_name)) + .setSubtitle(getString(enabled + ? R.string.title_legend_synchronize_on + : R.string.title_legend_synchronize_off)) + .setDeviceType(DeviceTypes.TYPE_GENERIC_ON_OFF) + .setStatus(Control.STATUS_OK) + .setControlTemplate(new ToggleTemplate( + DEVICE_SYNC, + new ControlButton(enabled, getString(R.string.title_widget_title_sync)) + )) + .build(); + + updatePublisher.onNext(control); + } + + return FlowAdapters.toFlowPublisher(updatePublisher); + } + + @Override + public void performControlAction(@NonNull String controlId, @NonNull ControlAction action, @NonNull Consumer consumer) { + if (action instanceof BooleanAction) { + consumer.accept(ControlAction.RESPONSE_OK); + + boolean enabled = ((BooleanAction) action).getNewState(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); + prefs.edit().putBoolean("enabled", enabled).apply(); + + Control control = new Control.StatefulBuilder(DEVICE_SYNC, getPendingIntent()) + .setCustomIcon(Icon.createWithResource(this, enabled + ? R.drawable.twotone_sync_24 + : R.drawable.twotone_sync_disabled_24)) + .setTitle(getString(R.string.app_name)) + .setSubtitle(getString(enabled + ? R.string.title_legend_synchronize_on + : R.string.title_legend_synchronize_off)) + .setDeviceType(DeviceTypes.TYPE_GENERIC_ON_OFF) + .setStatus(Control.STATUS_OK) + .setControlTemplate(new ToggleTemplate( + DEVICE_SYNC, + new ControlButton(enabled, getString(R.string.title_widget_title_sync)) + )) + .build(); + + updatePublisher.onNext(control); + } + } + + private PendingIntent getPendingIntent() { + Context context = getBaseContext(); + return PendingIntentCompat.getActivity( + context, + ActivityView.REQUEST_POWER, + new Intent(context, ActivityView.class), + PendingIntent.FLAG_UPDATE_CURRENT); + } +} diff --git a/app/src/play/AndroidManifest.xml b/app/src/play/AndroidManifest.xml index f23bdc8d5b..1ddd58213c 100644 --- a/app/src/play/AndroidManifest.xml +++ b/app/src/play/AndroidManifest.xml @@ -396,6 +396,15 @@ + + + + + +