Files
FairEmail/app/src/main/java/eu/faircode/email/ServiceTileSynchronize.java

88 lines
3.6 KiB
Java
Raw Normal View History

2018-11-07 12:10:58 +00:00
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/>.
2021-01-01 08:56:36 +01:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
2018-11-07 12:10:58 +00:00
*/
import android.annotation.TargetApi;
import android.content.SharedPreferences;
2018-11-09 16:05:10 +00:00
import android.graphics.drawable.Icon;
2018-11-07 12:10:58 +00:00
import android.os.Build;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
2019-03-15 13:54:25 +00:00
import androidx.preference.PreferenceManager;
2018-11-07 12:10:58 +00:00
@TargetApi(Build.VERSION_CODES.N)
public class ServiceTileSynchronize extends TileService implements SharedPreferences.OnSharedPreferenceChangeListener {
public void onStartListening() {
2018-12-24 12:27:45 +00:00
Log.i("Start tile synchronize");
2018-11-07 12:10:58 +00:00
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);
2019-02-14 12:47:02 +00:00
boolean enabled = prefs.getBoolean("enabled", true);
2018-12-24 12:27:45 +00:00
Log.i("Update tile synchronize=" + enabled);
2018-11-07 12:10:58 +00:00
Tile tile = getQsTile();
2020-07-05 21:38:15 +02:00
if (tile != null)
try {
tile.setState(enabled ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
tile.setIcon(Icon.createWithResource(this,
2020-09-28 09:18:56 +02:00
enabled ? R.drawable.twotone_sync_24 : R.drawable.twotone_sync_disabled_24));
2020-07-05 21:38:15 +02:00
tile.updateTile();
} catch (Throwable ex) {
Log.w(ex);
/*
java.lang.IllegalArgumentException: Service not registered: com.android.systemui.qs.external.TileLifecycleManager@9b4b3b0
at android.os.Parcel.createException(Parcel.java:1954)
at android.os.Parcel.readException(Parcel.java:1918)
at android.os.Parcel.readException(Parcel.java:1868)
at android.service.quicksettings.IQSService$Stub$Proxy.updateQsTile(IQSService.java:219)
at android.service.quicksettings.Tile.updateTile(Tile.java:182)
at eu.faircode.email.ServiceTileSynchronize.update(SourceFile:56)
at eu.faircode.email.ServiceTileSynchronize.onStartListening(SourceFile:37)
at android.service.quicksettings.TileService$H.handleMessage(TileService.java:407)
*/
}
2018-11-07 12:10:58 +00:00
}
public void onStopListening() {
2018-12-24 12:27:45 +00:00
Log.i("Stop tile synchronize");
2018-11-07 12:10:58 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
public void onClick() {
2018-12-24 12:27:45 +00:00
Log.i("Click tile synchronize");
2018-11-07 12:10:58 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
2019-02-14 12:47:02 +00:00
boolean enabled = !prefs.getBoolean("enabled", true);
2018-11-07 12:10:58 +00:00
prefs.edit().putBoolean("enabled", enabled).apply();
}
}