Skip to content

Commit

Permalink
CHANGELOG: Lockscreen Widgets: added ability to launch custom apps
Browse files Browse the repository at this point in the history
Signed-off-by: DHD2280 <[email protected]>
  • Loading branch information
DHD2280 committed Dec 4, 2024
1 parent 063c6a2 commit 08a578f
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
package it.dhd.oxygencustomizer.ui.fragments.mods.lockscreen;

import static it.dhd.oxygencustomizer.utils.Constants.Packages.SYSTEM_UI;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.EXTRA_WIDGET_1_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.EXTRA_WIDGET_2_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.EXTRA_WIDGET_3_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.EXTRA_WIDGET_4_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.LOCKSCREEN_WIDGETS;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.LOCKSCREEN_WIDGETS_EXTRAS;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.MAIN_WIDGET_1_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.MAIN_WIDGET_2_KEY;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.preference.ListPreference;
import androidx.preference.Preference;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import it.dhd.oxygencustomizer.R;
import it.dhd.oxygencustomizer.ui.adapters.PackageListAdapter;
import it.dhd.oxygencustomizer.ui.base.ControlledPreferenceFragmentCompat;
import it.dhd.oxygencustomizer.utils.WeatherScheduler;
import it.dhd.oxygencustomizer.weather.OmniJawsClient;
import it.dhd.oxygencustomizer.weather.WeatherConfig;

public class LockscreenWidgets extends ControlledPreferenceFragmentCompat {
public class LockscreenWidgets extends ControlledPreferenceFragmentCompat implements Preference.OnPreferenceChangeListener{

private OmniJawsClient mWeatherClient;

private static final String MAIN_WIDGET_1_KEY = "main_custom_widgets1";
private static final String MAIN_WIDGET_2_KEY = "main_custom_widgets2";
private static final String EXTRA_WIDGET_1_KEY = "custom_widgets1";
private static final String EXTRA_WIDGET_2_KEY = "custom_widgets2";
private static final String EXTRA_WIDGET_3_KEY = "custom_widgets3";
private static final String EXTRA_WIDGET_4_KEY = "custom_widgets4";
private PackageListAdapter mPackageAdapter;

private Map<Preference, String> widgetKeysMap = new HashMap<>();
private Map<Preference, String> initialWidgetKeysMap = new HashMap<>();

private Preference mMainWidget1;
private Preference mMainWidget2;
private Preference mExtraWidget1;
private Preference mExtraWidget2;
private Preference mExtraWidget3;
private Preference mExtraWidget4;
private Preference mDeviceInfoWidgetPref;
private ListPreference mMainWidget1;
private ListPreference mMainWidget2;
private ListPreference mExtraWidget1;
private ListPreference mExtraWidget2;
private ListPreference mExtraWidget3;
private ListPreference mExtraWidget4;
private ListPreference mDeviceInfoWidgetPref;

private List<Preference> mWidgetPreferences;
private List<ListPreference> mWidgetPreferences;

@Override
public String getTitle() {
Expand Down Expand Up @@ -77,6 +82,10 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
mWeatherClient = new OmniJawsClient(getContext());
mWeatherClient.queryWeather();

new Thread(() -> {
mPackageAdapter = new PackageListAdapter(requireActivity());
}).start();

mMainWidget1 = findPreference(MAIN_WIDGET_1_KEY);
mMainWidget2 = findPreference(MAIN_WIDGET_2_KEY);
mExtraWidget1 = findPreference(EXTRA_WIDGET_1_KEY);
Expand All @@ -93,6 +102,13 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
mExtraWidget3,
mExtraWidget4,
mDeviceInfoWidgetPref);

for (Preference widgetPref : mWidgetPreferences) {
if (widgetPref != null) {
widgetPref.setOnPreferenceChangeListener(this);
}
}

}

private List<String> replaceEmptyWithNone(List<String> inputList) {
Expand All @@ -109,12 +125,28 @@ private void saveInitialPreferences() {
}
}

private void pickApp(String preferenceKey) {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireActivity());
builder.setAdapter(mPackageAdapter, (dialog, which) -> {
PackageListAdapter.PackageItem info = mPackageAdapter.getItem(which);
mPreferences.putString(preferenceKey, "customapp:" + info.packageName);
savePrefs();
});
builder.setCancelable(false);
builder.setTitle(R.string.qs_widget_custom_app);
builder.show();
}

@Override
public void updateScreen(String key) {
super.updateScreen(key);

if (key == null) return;

savePrefs();
}

private void savePrefs() {
saveInitialPreferences();

List<String> mainWidgetsList = Arrays.asList(
Expand Down Expand Up @@ -150,7 +182,14 @@ public void updateScreen(String key) {
WeatherScheduler.scheduleUpdates(getContext());
WeatherScheduler.scheduleUpdateNow(getContext());
}

}

@Override
public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
if (newValue.equals("customapp")) {
pickApp(preference.getKey());
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,14 @@ public static class LockscreenClock {
};
}
public static class LockscreenWidgets {
// UI
public static final String MAIN_WIDGET_1_KEY = "main_custom_widgets1";
public static final String MAIN_WIDGET_2_KEY = "main_custom_widgets2";
public static final String EXTRA_WIDGET_1_KEY = "custom_widgets1";
public static final String EXTRA_WIDGET_2_KEY = "custom_widgets2";
public static final String EXTRA_WIDGET_3_KEY = "custom_widgets3";
public static final String EXTRA_WIDGET_4_KEY = "custom_widgets4";

public static final String LOCKSCREEN_WIDGETS_ENABLED = "lockscreen_widgets_enabled";
public static final String LOCKSCREEN_WIDGETS_DEVICE_WIDGET = "lockscreen_device_widget";
public static final String LOCKSCREEN_WIDGETS = "lockscreen_widgets";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenClock.LOCKSCREEN_CLOCK_SWITCH;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenClock.LOCKSCREEN_CLOCK_TEXT_SCALING;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenClock.LOCKSCREEN_CLOCK_TOP_MARGIN;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.EXTRA_WIDGET_1_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.EXTRA_WIDGET_2_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.EXTRA_WIDGET_3_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.EXTRA_WIDGET_4_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.LOCKSCREEN_WIDGETS;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.LOCKSCREEN_WIDGETS_BIG_ACTIVE;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.LOCKSCREEN_WIDGETS_BIG_ICON_ACTIVE;
Expand All @@ -96,6 +100,8 @@
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.LOCKSCREEN_WIDGETS_SMALL_ICON_INACTIVE;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.LOCKSCREEN_WIDGETS_SMALL_INACTIVE;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.LOCKSCREEN_WIDGETS_WEATHER_SETTINGS;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.MAIN_WIDGET_1_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.LockscreenWidgets.MAIN_WIDGET_2_KEY;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.QsHeaderClock.QS_HEADER_CLOCK_COLOR_CODE_ACCENT1;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.QsHeaderClock.QS_HEADER_CLOCK_COLOR_CODE_ACCENT2;
import static it.dhd.oxygencustomizer.utils.Constants.Preferences.QsHeaderClock.QS_HEADER_CLOCK_COLOR_CODE_ACCENT3;
Expand Down Expand Up @@ -1156,10 +1162,23 @@ public static void setupPreference(Preference preference) {
preference.setEnabled(isEnabled(key));

String summary = getSummary(preference.getContext(), key);
if (summary != null && !preference.getKey().equals("sb_illustration")) {
if (summary != null && !key.equals("sb_illustration")) {
preference.setSummary(summary);
}

if (key.equals(MAIN_WIDGET_1_KEY) ||
key.equals(MAIN_WIDGET_2_KEY) ||
key.equals(EXTRA_WIDGET_1_KEY) ||
key.equals(EXTRA_WIDGET_2_KEY) ||
key.equals(EXTRA_WIDGET_3_KEY) ||
key.equals(EXTRA_WIDGET_4_KEY)) {
String prefValue = instance.mPreferences.getString(key, "none");
if (prefValue.contains("customapp:")) {
preference.setSummaryProvider(preference1 -> preference1.getContext().getString(R.string.qs_widget_custom_app) + "\n" +
AppUtils.getAppName(preference1.getContext(), prefValue.replace("customapp:", "")));
}
}

if (preference instanceof OplusSliderPreference) {
((OplusSliderPreference) preference).slider.setLabelFormatter(value -> {
if (value == ((OplusSliderPreference) preference).defaultValue.get(0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@

import it.dhd.oxygencustomizer.BuildConfig;
import it.dhd.oxygencustomizer.R;
import it.dhd.oxygencustomizer.utils.AppUtils;
import it.dhd.oxygencustomizer.weather.OmniJawsClient;
import it.dhd.oxygencustomizer.xposed.hooks.systemui.ControllersProvider;
import it.dhd.oxygencustomizer.xposed.hooks.systemui.ThemeEnabler;
import it.dhd.oxygencustomizer.xposed.utils.ActivityLauncherUtils;
import it.dhd.oxygencustomizer.xposed.utils.DrawableConverter;
import it.dhd.oxygencustomizer.xposed.utils.ExtendedFAB;
import it.dhd.oxygencustomizer.xposed.utils.SystemUtils;

Expand Down Expand Up @@ -754,6 +756,27 @@ private boolean isNightMode() {
}

private void setUpWidgetWiews(ImageView iv, ExtendedFAB efab, String type) {
if (type.contains("customapp") && type.contains(":")) {
String[] split = type.split(":");
if (split.length == 2) {
String packageName = split[1];
Drawable appIcon = AppUtils.getAppIcon(mContext, packageName);
if (iv != null) {
iv.setTag("app");
iv.setImageDrawable(appIcon);
}
if (efab != null) {
efab.setTag("app");
efab.setIcon(DrawableConverter.scaleDrawable(mContext,
appIcon,
0.3f));
}
setUpWidgetResources(iv, efab, v -> {
mActivityLauncherUtils.launchApp(packageName, false);
vibrate(1);
}, null, AppUtils.getAppName(mContext, packageName));
}
}
switch (type) {
case "none":
if (iv != null) {
Expand Down Expand Up @@ -918,19 +941,26 @@ private void setUpWidgetWiews(ImageView iv, ExtendedFAB efab, String type) {
}
}

private void launchApp(String packageName) {
Intent launchIntent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
if (launchIntent == null) return;
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
mActivityLauncherUtils.launchApp(launchIntent);
}

private void setUpWidgetResources(ImageView iv, ExtendedFAB efab,
OnClickListener cl, Drawable icon, String text) {
if (efab != null) {
efab.setOnClickListener(cl);
efab.setIcon(icon);
if (icon != null) efab.setIcon(icon);
efab.setText(text);
if (mediaButtonFab == efab) {
attachSwipeGesture(efab);
}
}
if (iv != null) {
iv.setOnClickListener(cl);
iv.setImageDrawable(icon);
if (icon != null) iv.setImageDrawable(icon);
if (mediaButton == iv) {
attachSwipeGesture(iv);
}
Expand Down Expand Up @@ -998,10 +1028,14 @@ private void setButtonActiveState(ImageView iv, ExtendedFAB efab, boolean active
}
if (efab != null) {
efab.setBackgroundTintList(ColorStateList.valueOf(bgTint));
if (efab != weatherButtonFab) {
efab.setIconTint(ColorStateList.valueOf(tintColor));
} else {
if (efab.getTag() != null && efab.getTag().equals("app")) {
efab.setIconTint(null);
} else {
if (efab != weatherButtonFab) {
efab.setIconTint(ColorStateList.valueOf(tintColor));
} else {
efab.setIconTint(null);
}
}
efab.setTextColor(tintColor);
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@
<item>@string/camera</item>
<item>@string/wallet</item>
<item>@string/hotspot</item>
<item>@string/qs_widget_custom_app</item>
</string-array>

<string-array name="widget_values" translatable="false">
Expand All @@ -684,6 +685,7 @@
<item>camera</item>
<item>wallet</item>
<item>hotspot</item>
<item>customapp</item>
</string-array>

</resources>

0 comments on commit 08a578f

Please sign in to comment.