diff --git a/app/src/main/java/it/dhd/oxygencustomizer/xposed/utils/ActivityLauncherUtils.java b/app/src/main/java/it/dhd/oxygencustomizer/xposed/utils/ActivityLauncherUtils.java index 06e152bf1..a9d51c23d 100644 --- a/app/src/main/java/it/dhd/oxygencustomizer/xposed/utils/ActivityLauncherUtils.java +++ b/app/src/main/java/it/dhd/oxygencustomizer/xposed/utils/ActivityLauncherUtils.java @@ -13,6 +13,7 @@ import android.content.pm.ResolveInfo; import android.provider.MediaStore; import android.provider.Settings; +import android.text.TextUtils; import android.widget.Toast; import androidx.annotation.StringRes; @@ -21,6 +22,7 @@ import it.dhd.oxygencustomizer.BuildConfig; import it.dhd.oxygencustomizer.R; +import it.dhd.oxygencustomizer.utils.AppUtils; public class ActivityLauncherUtils { @@ -53,6 +55,19 @@ public void launchAppIfAvailable(Intent launchIntent, @StringRes int appTypeResI } } + public void launchAppIfAvailable(Intent launchIntent, String appName, boolean fromQs) { + final List apps = mPackageManager.queryIntentActivities(launchIntent, PackageManager.MATCH_DEFAULT_ONLY); + if (mActivityStarter == null) { + log("ActivityStarter is null"); + return; + } + if (!apps.isEmpty()) { + callMethod(mActivityStarter, fromQs ? "postStartActivityDismissingKeyguard" : "startActivity", launchIntent, fromQs ? 0 : false); + } else { + if (!TextUtils.isEmpty(appName)) showNoDefaultAppFoundToast(appName); + } + } + /** * Launches an app using the ActivityStarter * dismissing the shade. @@ -67,6 +82,26 @@ public void launchApp(Intent launchIntent) { callMethod(mActivityStarter, "postStartActivityDismissingKeyguard", launchIntent, 0 /* dismissShade */); } + /** + * Launches an app using the ActivityStarter + * dismissing the shade. + * Used for launching apps from Quick Settings. + * @param packageName The package name of the app + */ + public void launchApp(String packageName, boolean fromQs) { + if (mActivityStarter == null) { + log("ActivityStarter is null"); + return; + } + final Intent launchIntent = mPackageManager.getLaunchIntentForPackage(packageName); + if (launchIntent == null) { + log("Launch intent is null for package: " + packageName); + return; + } + launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP + Intent.FLAG_ACTIVITY_SINGLE_TOP); + launchAppIfAvailable(launchIntent, AppUtils.getAppName(mContext, packageName), fromQs); + } + /** * Launches the weather settings page {@link it.dhd.oxygencustomizer.ui.fragments.mods.WeatherSettings} * @param fromQs Whether the intent is launched from Quick Settings @@ -191,4 +226,8 @@ public void launchHotspotSettings() { private void showNoDefaultAppFoundToast(@StringRes int appTypeResId) { Toast.makeText(mContext, modRes.getString(appTypeResId) + " not found", Toast.LENGTH_SHORT).show(); } + + private void showNoDefaultAppFoundToast(String appName) { + Toast.makeText(mContext, appName + " not found", Toast.LENGTH_SHORT).show(); + } } diff --git a/app/src/main/java/it/dhd/oxygencustomizer/xposed/views/QsControlsView.java b/app/src/main/java/it/dhd/oxygencustomizer/xposed/views/QsControlsView.java index 1d5038d36..e3c5f6966 100644 --- a/app/src/main/java/it/dhd/oxygencustomizer/xposed/views/QsControlsView.java +++ b/app/src/main/java/it/dhd/oxygencustomizer/xposed/views/QsControlsView.java @@ -954,7 +954,7 @@ private void setUpWidgetWiews(ImageView iv, ExtendedFAB efab, String widgetType) 0.3f)); } setUpWidgetResources(iv, efab, - v -> launchApp(parts[1]), null, AppUtils.getAppName(mContext, parts[1])); + v -> mActivityLauncherUtils.launchApp(parts[1], true), null, AppUtils.getAppName(mContext, parts[1])); return; } } @@ -1286,13 +1286,6 @@ private void toggleHotspot() { vibrate(1); } - 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 boolean isWidgetEnabled(String widget) { return mWidgets.contains(widget); }