Skip to content

Commit 0039062

Browse files
empratyushmuhomorr
authored andcommitted
Reverse Wireless Charging UI
1 parent e89742b commit 0039062

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14592,4 +14592,7 @@
1459214592

1459314593
<string name="app_info_install_time">Installed: %1$s</string>
1459414594
<string name="app_info_update_time">Updated: %1$s</string>
14595+
14596+
<string name="battery_share_description">Charge other devices by placing them on the back of your phone</string>
14597+
<string name="battery_share">Battery share</string>
1459514598
</resources>

res/xml/power_usage_summary.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
android:summary="@string/battery_percentage_description"
6565
settings:controller="com.android.settings.display.BatteryPercentagePreferenceController" />
6666

67+
<SwitchPreference
68+
android:key="battery_share"
69+
android:title="@string/battery_share"
70+
android:summary="@string/battery_share_description"
71+
settings:controller="com.android.settings.display.BatterySharePreferenceController" />
72+
6773
<com.android.settingslib.widget.FooterPreference
6874
android:key="power_usage_footer"
6975
android:title="@string/battery_footer_summary"
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.android.settings.display;
2+
3+
import android.app.Activity;
4+
import android.content.BroadcastReceiver;
5+
import android.content.Context;
6+
import android.content.Intent;
7+
import android.content.IntentFilter;
8+
import android.os.BatteryManager;
9+
10+
import androidx.preference.Preference;
11+
import androidx.preference.PreferenceScreen;
12+
import androidx.preference.SwitchPreference;
13+
14+
import com.android.internal.R;
15+
import com.android.internal.batteryShare.ReverseWirelessCharger;
16+
import com.android.settings.core.BasePreferenceController;
17+
import com.android.settings.core.PreferenceControllerMixin;
18+
import com.android.settingslib.core.lifecycle.LifecycleObserver;
19+
import com.android.settingslib.core.lifecycle.events.OnStart;
20+
import com.android.settingslib.core.lifecycle.events.OnStop;
21+
22+
public class BatterySharePreferenceController extends BasePreferenceController implements
23+
PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver,
24+
OnStart, OnStop {
25+
26+
private static final String KEY_BATTERY_SHARE = "battery_share";
27+
private final ReverseWirelessCharger wirelessCharger;
28+
private final Context mContext;
29+
private Preference mPreference;
30+
31+
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
32+
@Override
33+
public void onReceive(Context context, Intent intent) {
34+
((Activity) mContext).runOnUiThread(() -> update());
35+
}
36+
};
37+
38+
39+
public BatterySharePreferenceController(Context context, String preferenceKey) {
40+
super(context, preferenceKey);
41+
mContext = context;
42+
wirelessCharger = ReverseWirelessCharger.getInstance();
43+
}
44+
45+
public boolean isPlugged(Context context) {
46+
Intent intent = context.registerReceiver(null,
47+
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
48+
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
49+
return plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
50+
}
51+
52+
@Override
53+
public void displayPreference(PreferenceScreen screen) {
54+
super.displayPreference(screen);
55+
mPreference = screen.findPreference(getPreferenceKey());
56+
update();
57+
}
58+
59+
@Override
60+
public String getPreferenceKey() {
61+
return KEY_BATTERY_SHARE;
62+
}
63+
64+
private void update() {
65+
if (mPreference == null) return;
66+
boolean enabled = !isPlugged(mContext) && wirelessCharger.isRtxSupported();
67+
mPreference.setEnabled(enabled);
68+
((SwitchPreference) mPreference).setChecked(wirelessCharger.isRtxModeOn());
69+
}
70+
71+
@Override
72+
public int getAvailabilityStatus() {
73+
return wirelessCharger.isRtxSupported() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
74+
}
75+
76+
@Override
77+
public void updateState(Preference preference) {
78+
update();
79+
}
80+
81+
@Override
82+
public boolean onPreferenceChange(Preference preference, Object newValue) {
83+
wirelessCharger.setRtxMode((Boolean) newValue);
84+
return true;
85+
}
86+
87+
@Override
88+
public void onStart() {
89+
IntentFilter filter = new IntentFilter();
90+
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
91+
mContext.registerReceiver(mBroadcastReceiver, filter);
92+
}
93+
94+
@Override
95+
public void onStop() {
96+
mContext.unregisterReceiver(mBroadcastReceiver);
97+
}
98+
}

0 commit comments

Comments
 (0)