diff --git a/app/src/main/java/it/dhd/oxygencustomizer/ui/fragments/mods/lockscreen/LockscreenWidgets.java b/app/src/main/java/it/dhd/oxygencustomizer/ui/fragments/mods/lockscreen/LockscreenWidgets.java index 68fe088c..43662ac6 100644 --- a/app/src/main/java/it/dhd/oxygencustomizer/ui/fragments/mods/lockscreen/LockscreenWidgets.java +++ b/app/src/main/java/it/dhd/oxygencustomizer/ui/fragments/mods/lockscreen/LockscreenWidgets.java @@ -1,14 +1,24 @@ 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; @@ -16,34 +26,29 @@ 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 widgetKeysMap = new HashMap<>(); private Map 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 mWidgetPreferences; + private List mWidgetPreferences; @Override public String getTitle() { @@ -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); @@ -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 replaceEmptyWithNone(List inputList) { @@ -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 mainWidgetsList = Arrays.asList( @@ -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; + } } diff --git a/app/src/main/java/it/dhd/oxygencustomizer/utils/Constants.java b/app/src/main/java/it/dhd/oxygencustomizer/utils/Constants.java index 6844b31c..9e120163 100644 --- a/app/src/main/java/it/dhd/oxygencustomizer/utils/Constants.java +++ b/app/src/main/java/it/dhd/oxygencustomizer/utils/Constants.java @@ -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"; diff --git a/app/src/main/java/it/dhd/oxygencustomizer/utils/PreferenceHelper.java b/app/src/main/java/it/dhd/oxygencustomizer/utils/PreferenceHelper.java index f019228c..ec2142f3 100644 --- a/app/src/main/java/it/dhd/oxygencustomizer/utils/PreferenceHelper.java +++ b/app/src/main/java/it/dhd/oxygencustomizer/utils/PreferenceHelper.java @@ -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; @@ -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; @@ -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)) diff --git a/app/src/main/java/it/dhd/oxygencustomizer/xposed/views/LockscreenWidgetsView.java b/app/src/main/java/it/dhd/oxygencustomizer/xposed/views/LockscreenWidgetsView.java index 2a48f434..edaa5f72 100644 --- a/app/src/main/java/it/dhd/oxygencustomizer/xposed/views/LockscreenWidgetsView.java +++ b/app/src/main/java/it/dhd/oxygencustomizer/xposed/views/LockscreenWidgetsView.java @@ -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; @@ -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) { @@ -918,11 +941,18 @@ 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); @@ -930,7 +960,7 @@ private void setUpWidgetResources(ImageView iv, ExtendedFAB efab, } if (iv != null) { iv.setOnClickListener(cl); - iv.setImageDrawable(icon); + if (icon != null) iv.setImageDrawable(icon); if (mediaButton == iv) { attachSwipeGesture(iv); } @@ -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); } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index ed9af6cc..e9360ffd 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -667,6 +667,7 @@ @string/camera @string/wallet @string/hotspot + @string/qs_widget_custom_app @@ -684,6 +685,7 @@ camera wallet hotspot + customapp \ No newline at end of file