Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class QuickSettingsTileForce extends TileService {
public QuickSettingsTileForce() {

}

private Context mContext;
private SharedPreferences mPreferences; // Manually initialized - no injection needed
private Resources res;
Expand Down Expand Up @@ -89,18 +90,18 @@ private void updateTileState(Tile tile, int force) {
case Constants.BTNSTATE_FORCE_START:
tile.setLabel(res.getString(R.string.qs_forced_to_run));
tile.setState(Tile.STATE_ACTIVE);
tile.setIcon(Icon.createWithResource(mContext,R.drawable.ic_qs_forced_to_run));
tile.setIcon(Icon.createWithResource(mContext, R.drawable.ic_qs_forced_to_run));
break;
case Constants.BTNSTATE_FORCE_STOP:
tile.setLabel(res.getString(R.string.qs_forced_to_stop));
tile.setState(Tile.STATE_ACTIVE);
tile.setIcon(Icon.createWithResource(mContext,R.drawable.ic_qs_forced_to_stop));
tile.setIcon(Icon.createWithResource(mContext, R.drawable.ic_qs_forced_to_stop));
break;
case Constants.BTNSTATE_NO_FORCE_START_STOP:
default:
tile.setLabel(res.getString(R.string.qs_following_run_conditions));
tile.setState(Tile.STATE_INACTIVE);
tile.setIcon(Icon.createWithResource(mContext,R.drawable.ic_qs_force));
tile.setIcon(Icon.createWithResource(mContext, R.drawable.ic_qs_force));
}
tile.updateTile();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,62 @@
public class QuickSettingsTileSchedule extends TileService implements ServiceConnection, SyncthingService.OnServiceStateChangeListener {

private static final String TAG = "QuickSettingsTileSchedule";
private static final boolean ENABLE_VERBOSE_LOG = false;

public QuickSettingsTileSchedule() {

}
private int mTilesAvailableState = Tile.STATE_INACTIVE;

private Context mContext;
private SharedPreferences mPreferences; // Manually initialized - no injection needed

private final SharedPreferences.OnSharedPreferenceChangeListener mPrefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String pref) {
if (pref == null || !pref.equals(Constants.PREF_BTNSTATE_FORCE_START_STOP)) return;
refreshTile();
}
};
private SyncthingService mSyncthingService;

private int tile_active_state = Tile.STATE_INACTIVE;
public QuickSettingsTileSchedule() {
}


@Override
public void onDestroy() {
LogV("onDestroy()");
if (mSyncthingService != null) {
mSyncthingService.unregisterOnServiceStateChangeListener(this);
mSyncthingService = null;
}
if (mPreferences != null) {
mPreferences.unregisterOnSharedPreferenceChangeListener(mPrefListener);
}
try {
if (mContext != null) {
mContext.unbindService(this);
}
} catch (IllegalArgumentException | IllegalStateException e) {
LogV("Service not bound or already unbound");
}
super.onDestroy();
}

@Override
public void onStartListening() {
Log.d(TAG, "onStartListening()");
Tile tile = getQsTile();
if (tile != null) {
LogV("onStartListening()");
if (getQsTile() != null) {
mContext = getApplication().getApplicationContext();
Resources res = mContext.getResources();
mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
mPreferences.registerOnSharedPreferenceChangeListener(mPrefListener);

// look through running services to see whether the app is currently running
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
boolean syncthingRunning = false;
for (ActivityManager.RunningServiceInfo service : am.getRunningServices(Integer.MAX_VALUE)) {
if (SyncthingService.class.getName().equals(service.service.getClassName())) {
syncthingRunning = true;
break;
}
}

// disable tile if app is not running, schedule is off, or syncthing is force-started/stopped
if (!syncthingRunning || !mPreferences.getBoolean(Constants.PREF_RUN_ON_TIME_SCHEDULE, false)
|| mPreferences.getInt(Constants.PREF_BTNSTATE_FORCE_START_STOP, Constants.BTNSTATE_NO_FORCE_START_STOP) != Constants.BTNSTATE_NO_FORCE_START_STOP) {
tile.setState(Tile.STATE_UNAVAILABLE);
tile.setLabel(res.getString(R.string.qs_schedule_disabled));
tile.updateTile();
return;
try {
Intent bindIntent = new Intent(mContext, SyncthingService.class);
mContext.bindService(bindIntent, this, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
Log.w(TAG, "Failed to bind to SyncthingService", e);
}

// try to bind to SyncthingService if it's running and not already bound
if (mSyncthingService == null) {
try {
Intent bindIntent = new Intent(mContext, SyncthingService.class);
mContext.bindService(bindIntent, this, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
Log.w(TAG, "Failed to bind to SyncthingService", e);
}
}

tile.setState(tile_active_state);
tile.setLabel(res.getString(
R.string.qs_schedule_label_minutes,
Integer.parseInt(mPreferences.getString(Constants.PREF_SYNC_DURATION_MINUTES, "5"))
));
tile.updateTile();
refreshTile();
}
super.onStartListening();
}
Expand All @@ -94,10 +95,39 @@ public void onClick() {
localBroadcastManager.sendBroadcast(intent);
}

private void refreshTile() {
if (setTileUnavailable()) {
return;
}
updateTile(mTilesAvailableState);
}

private boolean setTileUnavailable() {
Tile tile = getQsTile();
if (tile == null) return false;

// look through running services to see whether the app is currently running
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
boolean syncthingRunning = false;
for (ActivityManager.RunningServiceInfo service : am.getRunningServices(Integer.MAX_VALUE)) {
if (SyncthingService.class.getName().equals(service.service.getClassName())) {
syncthingRunning = true;
break;
}
}

// disable tile if app is not running, schedule is off, or syncthing is force-started/stopped
if (syncthingRunning && mPreferences.getBoolean(Constants.PREF_RUN_ON_TIME_SCHEDULE, false) && mPreferences.getInt(Constants.PREF_BTNSTATE_FORCE_START_STOP, Constants.BTNSTATE_NO_FORCE_START_STOP) == Constants.BTNSTATE_NO_FORCE_START_STOP) {
return false;
}

updateTile(Tile.STATE_UNAVAILABLE);
return true;
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, String.format("onServiceConnected(ComponentName=%s, IBinder=%s)", name.toString(), service.toString()));
LogV(String.format("onServiceConnected(ComponentName=%s, IBinder=%s)", name.toString(), service.toString()));
SyncthingServiceBinder syncthingServiceBinder = (SyncthingServiceBinder) service;
mSyncthingService = syncthingServiceBinder.getService();
mSyncthingService.registerOnServiceStateChangeListener(this);
Expand All @@ -108,38 +138,38 @@ public void onServiceDisconnected(ComponentName componentName) {
mSyncthingService = null;
}

@Override
public void onDestroy() {
Log.d(TAG, "onDestroy()");
if (mSyncthingService != null) {
mSyncthingService.unregisterOnServiceStateChangeListener(this);
}
try {
if (mContext != null) {
mContext.unbindService(this);
}
} catch (IllegalArgumentException | IllegalStateException e) {
Log.d(TAG, "Service not bound or already unbound");
private void updateTile(int newState) {
Tile tile = getQsTile();
if (tile == null) return;
if (newState == tile.getState()) return;

tile.setState(newState);

String label;
Resources res = mContext.getResources();
if (newState == Tile.STATE_INACTIVE || newState == Tile.STATE_ACTIVE) {
label = res.getString(R.string.qs_schedule_label_minutes, Integer.parseInt(mPreferences.getString(Constants.PREF_SYNC_DURATION_MINUTES, "5")));
} else {
label = res.getString(R.string.qs_schedule_disabled);
}
mSyncthingService = null;
tile_active_state = Tile.STATE_INACTIVE;
super.onDestroy();
tile.setLabel(label);

tile.updateTile();
}

@Override
public void onServiceStateChange(SyncthingService.State currentState) {
Log.d(TAG, String.format("onServiceStateChange: %s", currentState.toString()));
LogV(String.format("onServiceStateChange: %s", currentState.toString()));

int tile_active_state = Tile.STATE_INACTIVE;
mTilesAvailableState = Tile.STATE_INACTIVE;
if (currentState == SyncthingService.State.STARTING || currentState == SyncthingService.State.ACTIVE) {
tile_active_state = Tile.STATE_ACTIVE;
mTilesAvailableState = Tile.STATE_ACTIVE;
}
if (tile_active_state == this.tile_active_state) return;

Tile tile = getQsTile();
this.tile_active_state = tile_active_state;
tile.setState(tile_active_state);
tile.updateTile();
refreshTile();
}

private void LogV(String logMessage) {
if (!ENABLE_VERBOSE_LOG) return;
Log.v(TAG, logMessage);
}
}