Skip to content

Commit

Permalink
Reverse Wireless Charging UI
Browse files Browse the repository at this point in the history
  • Loading branch information
empratyush authored and muhomorr committed Aug 18, 2023
1 parent e89742b commit 0039062
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14592,4 +14592,7 @@

<string name="app_info_install_time">Installed: %1$s</string>
<string name="app_info_update_time">Updated: %1$s</string>

<string name="battery_share_description">Charge other devices by placing them on the back of your phone</string>
<string name="battery_share">Battery share</string>
</resources>
6 changes: 6 additions & 0 deletions res/xml/power_usage_summary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
android:summary="@string/battery_percentage_description"
settings:controller="com.android.settings.display.BatteryPercentagePreferenceController" />

<SwitchPreference
android:key="battery_share"
android:title="@string/battery_share"
android:summary="@string/battery_share_description"
settings:controller="com.android.settings.display.BatterySharePreferenceController" />

<com.android.settingslib.widget.FooterPreference
android:key="power_usage_footer"
android:title="@string/battery_footer_summary"
Expand Down
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);
}
}

0 comments on commit 0039062

Please sign in to comment.