Added global on/off settings tile

pull/146/head
M66B 6 years ago
parent 7a4efaab2e
commit 5ce4282dee

@ -112,9 +112,19 @@
android:permission="android.permission.BIND_JOB_SERVICE" />
<service
android:name=".ServiceTile"
android:name=".ServiceTileSynchronize"
android:icon="@drawable/baseline_compare_arrows_24"
android:label="@string/tile_synchronize"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>
<service
android:name=".ServiceTileUnseen"
android:icon="@drawable/baseline_mail_24"
android:label="@string/app_name"
android:label="@string/tile_unseen"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />

@ -0,0 +1,74 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.annotation.TargetApi;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.util.Log;
@TargetApi(Build.VERSION_CODES.N)
public class ServiceTileSynchronize extends TileService implements SharedPreferences.OnSharedPreferenceChangeListener {
public void onStartListening() {
Log.i(Helper.TAG, "Start tile synchronize");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
update();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if ("enabled".equals(key))
update();
}
private void update() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean enabled = prefs.getBoolean("enabled", false);
Log.i(Helper.TAG, "Update tile synchronize=" + enabled);
Tile tile = getQsTile();
if (tile != null) {
tile.setState(enabled ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
tile.updateTile();
}
}
public void onStopListening() {
Log.i(Helper.TAG, "Stop tile synchronize");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
public void onClick() {
Log.i(Helper.TAG, "Click tile synchronize");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean enabled = !prefs.getBoolean("enabled", false);
prefs.edit().putBoolean("enabled", enabled).apply();
if (enabled)
ServiceSynchronize.start(this);
else
ServiceSynchronize.stop(this);
}
}

@ -33,7 +33,7 @@ import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.Observer;
@TargetApi(Build.VERSION_CODES.N)
public class ServiceTile extends TileService {
public class ServiceTileUnseen extends TileService {
LifecycleService owner = new LifecycleService();
@Override
@ -61,12 +61,14 @@ public class ServiceTile extends TileService {
}
public void onStartListening() {
Log.i(Helper.TAG, "Tile start");
Log.i(Helper.TAG, "Start tile unseen");
DB db = DB.getInstance(this);
db.message().liveUnseenUnified().observe(owner, new Observer<List<TupleMessageEx>>() {
@Override
public void onChanged(List<TupleMessageEx> messages) {
Log.i(Helper.TAG, "Update tile unseen=" + messages.size());
Tile tile = getQsTile();
if (tile != null) {
tile.setState(messages.size() > 0 ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
@ -79,14 +81,14 @@ public class ServiceTile extends TileService {
}
public void onStopListening() {
Log.i(Helper.TAG, "Tile stop");
Log.i(Helper.TAG, "Stop tile unseen");
DB db = DB.getInstance(this);
db.message().liveUnseenUnified().removeObservers(owner);
}
public void onClick() {
Log.i(Helper.TAG, "Tile click");
Log.i(Helper.TAG, "Click tile unseen");
Intent clear = new Intent(this, ServiceSynchronize.class);
clear.setAction("clear");

@ -7,6 +7,9 @@
<string name="channel_notification">Notifications</string>
<string name="channel_error">Errors</string>
<string name="tile_synchronize">Synchronize</string>
<string name="tile_unseen">New messages</string>
<string name="shortcut_compose">Compose</string>
<plurals name="title_notification_synchronizing">

Loading…
Cancel
Save