Skip to content

Commit

Permalink
refract launchApp method
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 5a1efc0 commit 063c6a2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +22,7 @@

import it.dhd.oxygencustomizer.BuildConfig;
import it.dhd.oxygencustomizer.R;
import it.dhd.oxygencustomizer.utils.AppUtils;

public class ActivityLauncherUtils {

Expand Down Expand Up @@ -53,6 +55,19 @@ public void launchAppIfAvailable(Intent launchIntent, @StringRes int appTypeResI
}
}

public void launchAppIfAvailable(Intent launchIntent, String appName, boolean fromQs) {
final List<ResolveInfo> 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.
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 063c6a2

Please sign in to comment.