Skip to content

Commit

Permalink
see 03/09 log
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankj committed Mar 9, 2018
1 parent d9ec61a commit 402e734
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 45 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dependencies {
// LeakCanary
debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakcanary_version"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakcanary_version"
// implementation 'com.blankj:utilcode:1.13.1'
// implementation 'com.blankj:utilcode:1.13.2'
}


46 changes: 22 additions & 24 deletions utilcode/src/main/java/com/blankj/utilcode/util/ActivityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ public static void startHomeActivity() {
* @return the list of activity
*/
public static List<Activity> getActivityList() {
return Utils.sActivityList;
return Utils.getActivityList();
}

/**
Expand Down Expand Up @@ -739,11 +739,9 @@ public static String getLauncherActivity(@NonNull final String pkg) {
* @return the top activity in activity's stack
*/
public static Activity getTopActivity() {
if (Utils.sTopActivityWeakRef != null) {
final Activity activity = Utils.sTopActivityWeakRef.get();
if (activity != null) {
return activity;
}
final Activity topActivity = Utils.getActivityList().getLast();
if (topActivity != null) {
return topActivity;
}
// using reflect to get top activity
try {
Expand All @@ -761,9 +759,9 @@ public static Activity getTopActivity() {
if (!pausedField.getBoolean(activityRecord)) {
Field activityField = activityRecordClass.getDeclaredField("activity");
activityField.setAccessible(true);
Activity topActivity = (Activity) activityField.get(activityRecord);
Utils.setTopActivityWeakRef(topActivity);
return topActivity;
Activity activity = (Activity) activityField.get(activityRecord);
Utils.setTopActivity(activity);
return activity;
}
}
} catch (ClassNotFoundException e) {
Expand All @@ -777,7 +775,7 @@ public static Activity getTopActivity() {
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
throw new NullPointerException("The top activity is null.");
return null;
}

/**
Expand All @@ -787,7 +785,7 @@ public static Activity getTopActivity() {
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isActivityExistsInStack(@NonNull final Activity activity) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (Activity aActivity : activities) {
if (aActivity.equals(activity)) {
return true;
Expand All @@ -803,7 +801,7 @@ public static boolean isActivityExistsInStack(@NonNull final Activity activity)
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isActivityExistsInStack(@NonNull final Class<?> clz) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (Activity aActivity : activities) {
if (aActivity.getClass().equals(clz)) {
return true;
Expand Down Expand Up @@ -866,7 +864,7 @@ public static void finishActivity(@NonNull final Class<?> clz) {
* @param isLoadAnim True to use animation for the outgoing activity, false otherwise.
*/
public static void finishActivity(@NonNull final Class<?> clz, final boolean isLoadAnim) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (Activity activity : activities) {
if (activity.getClass().equals(clz)) {
activity.finish();
Expand All @@ -889,7 +887,7 @@ public static void finishActivity(@NonNull final Class<?> clz, final boolean isL
public static void finishActivity(@NonNull final Class<?> clz,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (Activity activity : activities) {
if (activity.getClass().equals(clz)) {
activity.finish();
Expand Down Expand Up @@ -919,7 +917,7 @@ public static boolean finishToActivity(@NonNull final Activity activity,
public static boolean finishToActivity(@NonNull final Activity activity,
final boolean isIncludeSelf,
final boolean isLoadAnim) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; --i) {
Activity aActivity = activities.get(i);
if (aActivity.equals(activity)) {
Expand Down Expand Up @@ -947,7 +945,7 @@ public static boolean finishToActivity(@NonNull final Activity activity,
final boolean isIncludeSelf,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; --i) {
Activity aActivity = activities.get(i);
if (aActivity.equals(activity)) {
Expand Down Expand Up @@ -982,7 +980,7 @@ public static boolean finishToActivity(@NonNull final Class<?> clz,
public static boolean finishToActivity(@NonNull final Class<?> clz,
final boolean isIncludeSelf,
final boolean isLoadAnim) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; --i) {
Activity aActivity = activities.get(i);
if (aActivity.getClass().equals(clz)) {
Expand Down Expand Up @@ -1010,7 +1008,7 @@ public static boolean finishToActivity(@NonNull final Class<?> clz,
final boolean isIncludeSelf,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; --i) {
Activity aActivity = activities.get(i);
if (aActivity.getClass().equals(clz)) {
Expand Down Expand Up @@ -1042,7 +1040,7 @@ public static void finishOtherActivities(@NonNull final Class<?> clz) {
*/
public static void finishOtherActivities(@NonNull final Class<?> clz,
final boolean isLoadAnim) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; i--) {
Activity activity = activities.get(i);
if (!activity.getClass().equals(clz)) {
Expand All @@ -1063,7 +1061,7 @@ public static void finishOtherActivities(@NonNull final Class<?> clz,
public static void finishOtherActivities(@NonNull final Class<?> clz,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 1; i >= 0; i--) {
Activity activity = activities.get(i);
if (!activity.getClass().equals(clz)) {
Expand All @@ -1085,7 +1083,7 @@ public static void finishAllActivities() {
* @param isLoadAnim True to use animation for the outgoing activity, false otherwise.
*/
public static void finishAllActivities(final boolean isLoadAnim) {
List<Activity> activityList = Utils.sActivityList;
List<Activity> activityList = Utils.getActivityList();
for (int i = activityList.size() - 1; i >= 0; --i) {// remove from top
Activity activity = activityList.get(i);
// sActivityList remove the index activity at onActivityDestroyed
Expand All @@ -1106,7 +1104,7 @@ public static void finishAllActivities(final boolean isLoadAnim) {
*/
public static void finishAllActivities(@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activityList = Utils.sActivityList;
List<Activity> activityList = Utils.getActivityList();
for (int i = activityList.size() - 1; i >= 0; --i) {// remove from top
Activity activity = activityList.get(i);
// sActivityList remove the index activity at onActivityDestroyed
Expand All @@ -1128,7 +1126,7 @@ public static void finishAllActivitiesExceptNewest() {
* @param isLoadAnim True to use animation for the outgoing activity, false otherwise.
*/
public static void finishAllActivitiesExceptNewest(final boolean isLoadAnim) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 2; i >= 0; i--) {
finishActivity(activities.get(i), isLoadAnim);
}
Expand All @@ -1144,7 +1142,7 @@ public static void finishAllActivitiesExceptNewest(final boolean isLoadAnim) {
*/
public static void finishAllActivitiesExceptNewest(@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
List<Activity> activities = Utils.sActivityList;
List<Activity> activities = Utils.getActivityList();
for (int i = activities.size() - 2; i >= 0; i--) {
finishActivity(activities.get(i), enterAnim, exitAnim);
}
Expand Down
7 changes: 4 additions & 3 deletions utilcode/src/main/java/com/blankj/utilcode/util/AppUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,10 @@ public static boolean isAppSystem(final String packageName) {
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isAppForeground() {
ActivityManager manager =
ActivityManager am =
(ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> info = manager.getRunningAppProcesses();
if (am == null) return false;
List<ActivityManager.RunningAppProcessInfo> info = am.getRunningAppProcesses();
if (info == null || info.size() == 0) return false;
for (ActivityManager.RunningAppProcessInfo aInfo : info) {
if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
Expand Down Expand Up @@ -376,7 +377,7 @@ public static void launchAppDetailsSettings(final String packageName) {
* Exit the application.
*/
public static void exitApp() {
List<Activity> activityList = Utils.sActivityList;
List<Activity> activityList = Utils.getActivityList();
for (int i = activityList.size() - 1; i >= 0; --i) {// remove from top
Activity activity = activityList.get(i);
// sActivityList remove the index activity at onActivityDestroyed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ private ProcessUtils() {
* @return the foreground process name
*/
public static String getForegroundProcessName() {
ActivityManager manager =
ActivityManager am =
(ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
if (manager == null) return null;
List<ActivityManager.RunningAppProcessInfo> pInfo = manager.getRunningAppProcesses();
if (am == null) return null;
List<ActivityManager.RunningAppProcessInfo> pInfo = am.getRunningAppProcesses();
if (pInfo != null && pInfo.size() > 0) {
for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) {
if (aInfo.importance
Expand All @@ -57,10 +57,10 @@ public static String getForegroundProcessName() {
}
}
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
PackageManager packageManager = Utils.getApp().getPackageManager();
PackageManager pm = Utils.getApp().getPackageManager();
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Log.i("ProcessUtils", list.toString());
if (list.size() <= 0) {
Log.i("ProcessUtils",
Expand All @@ -69,7 +69,7 @@ public static String getForegroundProcessName() {
}
try {// 有"有权查看使用权限的应用"选项
ApplicationInfo info =
packageManager.getApplicationInfo(Utils.getApp().getPackageName(), 0);
pm.getApplicationInfo(Utils.getApp().getPackageName(), 0);
AppOpsManager aom =
(AppOpsManager) Utils.getApp().getSystemService(Context.APP_OPS_SERVICE);
if (aom != null) {
Expand Down
10 changes: 10 additions & 0 deletions utilcode/src/main/java/com/blankj/utilcode/util/ServiceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ public static void unbindService(final ServiceConnection conn) {
Utils.getApp().unbindService(conn);
}

/**
* Return whether service is running.
*
* @param cls The service class.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isServiceRunning(final Class<?> cls) {
return isServiceRunning(cls.getName());
}

/**
* Return whether service is running.
*
Expand Down
27 changes: 16 additions & 11 deletions utilcode/src/main/java/com/blankj/utilcode/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import android.os.Bundle;
import android.support.annotation.NonNull;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;

/**
* <pre>
Expand All @@ -36,24 +34,22 @@ public final class Utils {
@SuppressLint("StaticFieldLeak")
private static Application sApplication;

static WeakReference<Activity> sTopActivityWeakRef;
static List<Activity> sActivityList = new LinkedList<>();
static LinkedList<Activity> sActivityList = new LinkedList<>();

private static ActivityLifecycleCallbacks mCallbacks = new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
sActivityList.add(activity);
setTopActivityWeakRef(activity);
setTopActivity(activity);
}

@Override
public void onActivityStarted(Activity activity) {
setTopActivityWeakRef(activity);
setTopActivity(activity);
}

@Override
public void onActivityResumed(Activity activity) {
setTopActivityWeakRef(activity);
setTopActivity(activity);
}

@Override
Expand Down Expand Up @@ -102,10 +98,19 @@ public static Application getApp() {
throw new NullPointerException("u should init first");
}

static void setTopActivityWeakRef(final Activity activity) {
static void setTopActivity(final Activity activity) {
if (activity.getClass() == PermissionUtils.PermissionActivity.class) return;
if (sTopActivityWeakRef == null || !activity.equals(sTopActivityWeakRef.get())) {
sTopActivityWeakRef = new WeakReference<>(activity);
if (sActivityList.contains(activity)) {
if (!sActivityList.getLast().equals(activity)) {
sActivityList.remove(activity);
sActivityList.addLast(activity);
}
} else {
sActivityList.addLast(activity);
}
}

public static LinkedList<Activity> getActivityList() {
return sActivityList;
}
}

0 comments on commit 402e734

Please sign in to comment.