Added power control

This commit is contained in:
M66B 2021-05-21 23:11:58 +02:00
parent b6261910dd
commit 751fe8222c
7 changed files with 163 additions and 1 deletions

View File

@ -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"
}

View File

@ -395,6 +395,15 @@
</intent-filter>
</service>
<service
android:name=".ServicePowerControl"
android:label="@string/app_name"
android:permission="android.permission.BIND_CONTROLS">
<intent-filter>
<action android:name="android.service.controls.ControlsProviderService" />
</intent-filter>
</service>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}"

View File

@ -395,6 +395,15 @@
</intent-filter>
</service>
<service
android:name=".ServicePowerControl"
android:label="@string/app_name"
android:permission="android.permission.BIND_CONTROLS">
<intent-filter>
<action android:name="android.service.controls.ControlsProviderService" />
</intent-filter>
</service>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}"

View File

@ -397,6 +397,15 @@
</intent-filter>
</service>
<service
android:name=".ServicePowerControl"
android:label="@string/app_name"
android:permission="android.permission.BIND_CONTROLS">
<intent-filter>
<action android:name="android.service.controls.ControlsProviderService" />
</intent-filter>
</service>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}"

View File

@ -41,7 +41,6 @@ import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
@ -128,6 +127,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
static final int REQUEST_ERROR = 6;
static final int REQUEST_UPDATE = 7;
static final int REQUEST_WIDGET = 8;
static final int REQUEST_POWER = 9;
static final String ACTION_VIEW_FOLDERS = BuildConfig.APPLICATION_ID + ".VIEW_FOLDERS";
static final String ACTION_VIEW_MESSAGES = BuildConfig.APPLICATION_ID + ".VIEW_MESSAGES";

View File

@ -0,0 +1,119 @@
package eu.faircode.email;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.service.controls.Control;
import android.service.controls.ControlsProviderService;
import android.service.controls.DeviceTypes;
import android.service.controls.actions.BooleanAction;
import android.service.controls.actions.ControlAction;
import android.service.controls.templates.ControlButton;
import android.service.controls.templates.ToggleTemplate;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.preference.PreferenceManager;
import org.reactivestreams.FlowAdapters;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Flow;
import java.util.function.Consumer;
import io.reactivex.Flowable;
import io.reactivex.processors.ReplayProcessor;
@RequiresApi(api = Build.VERSION_CODES.R)
public class ServicePowerControl extends ControlsProviderService {
private ReplayProcessor updatePublisher;
private static String DEVICE_SYNC = BuildConfig.APPLICATION_ID + ".sync";
@NonNull
@Override
public Flow.Publisher<Control> 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<Control> createPublisherFor(@NonNull List<String> 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<Integer> 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);
}
}

View File

@ -396,6 +396,15 @@
</intent-filter>
</service>
<service
android:name=".ServicePowerControl"
android:label="@string/app_name"
android:permission="android.permission.BIND_CONTROLS">
<intent-filter>
<action android:name="android.service.controls.ControlsProviderService" />
</intent-filter>
</service>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}"