-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e89742b
commit 0039062
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/com/android/settings/display/BatterySharePreferenceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.android.settings.display; | ||
|
||
import android.app.Activity; | ||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.os.BatteryManager; | ||
|
||
import androidx.preference.Preference; | ||
import androidx.preference.PreferenceScreen; | ||
import androidx.preference.SwitchPreference; | ||
|
||
import com.android.internal.R; | ||
import com.android.internal.batteryShare.ReverseWirelessCharger; | ||
import com.android.settings.core.BasePreferenceController; | ||
import com.android.settings.core.PreferenceControllerMixin; | ||
import com.android.settingslib.core.lifecycle.LifecycleObserver; | ||
import com.android.settingslib.core.lifecycle.events.OnStart; | ||
import com.android.settingslib.core.lifecycle.events.OnStop; | ||
|
||
public class BatterySharePreferenceController extends BasePreferenceController implements | ||
PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver, | ||
OnStart, OnStop { | ||
|
||
private static final String KEY_BATTERY_SHARE = "battery_share"; | ||
private final ReverseWirelessCharger wirelessCharger; | ||
private final Context mContext; | ||
private Preference mPreference; | ||
|
||
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
((Activity) mContext).runOnUiThread(() -> update()); | ||
} | ||
}; | ||
|
||
|
||
public BatterySharePreferenceController(Context context, String preferenceKey) { | ||
super(context, preferenceKey); | ||
mContext = context; | ||
wirelessCharger = ReverseWirelessCharger.getInstance(); | ||
} | ||
|
||
public boolean isPlugged(Context context) { | ||
Intent intent = context.registerReceiver(null, | ||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); | ||
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); | ||
return plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS; | ||
} | ||
|
||
@Override | ||
public void displayPreference(PreferenceScreen screen) { | ||
super.displayPreference(screen); | ||
mPreference = screen.findPreference(getPreferenceKey()); | ||
update(); | ||
} | ||
|
||
@Override | ||
public String getPreferenceKey() { | ||
return KEY_BATTERY_SHARE; | ||
} | ||
|
||
private void update() { | ||
if (mPreference == null) return; | ||
boolean enabled = !isPlugged(mContext) && wirelessCharger.isRtxSupported(); | ||
mPreference.setEnabled(enabled); | ||
((SwitchPreference) mPreference).setChecked(wirelessCharger.isRtxModeOn()); | ||
} | ||
|
||
@Override | ||
public int getAvailabilityStatus() { | ||
return wirelessCharger.isRtxSupported() ? AVAILABLE : UNSUPPORTED_ON_DEVICE; | ||
} | ||
|
||
@Override | ||
public void updateState(Preference preference) { | ||
update(); | ||
} | ||
|
||
@Override | ||
public boolean onPreferenceChange(Preference preference, Object newValue) { | ||
wirelessCharger.setRtxMode((Boolean) newValue); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
IntentFilter filter = new IntentFilter(); | ||
filter.addAction(Intent.ACTION_BATTERY_CHANGED); | ||
mContext.registerReceiver(mBroadcastReceiver, filter); | ||
} | ||
|
||
@Override | ||
public void onStop() { | ||
mContext.unregisterReceiver(mBroadcastReceiver); | ||
} | ||
} |