Java Code Examples for android.content.ComponentCallbacks2#TRIM_MEMORY_UI_HIDDEN

The following examples show how to use android.content.ComponentCallbacks2#TRIM_MEMORY_UI_HIDDEN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LockscreenHandler.java    From AndroidAppLockscreen with MIT License 6 votes vote down vote up
@Override
public void onTrimMemory(int i) {
    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    Log.d("topActivity", "CURRENT Activity ::"
            + taskInfo.get(0).topActivity.getClassName());

    if (taskInfo != null && taskInfo.size() > 0) {
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        packageName = componentInfo.getPackageName();
    }
    if (!packageName.equals(getPackageName()) && i == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
        // We're in the Background
        wentToBg = true;
        Log.d(TAG, "wentToBg: " + wentToBg);
    }
}
 
Example 2
Source File: VRBrowserActivity.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void onTrimMemory(int level) {

    // Determine which lifecycle or system event was raised.
    switch (level) {

        case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
        case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
        case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
        case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
            // Curently ignore these levels. They are handled somewhere else.
            break;
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:
            // It looks like these come in all at the same time so just always suspend inactive Sessions.
            Log.d(LOGTAG, "Memory pressure, suspending inactive sessions.");
            SessionStore.get().suspendAllInactiveSessions();
            break;
        default:
            Log.e(LOGTAG, "onTrimMemory unknown level: " + level);
            break;
    }
}
 
Example 3
Source File: DownloadService.java    From Audinaut with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onTrimMemory(int level) {
    ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(this);
    if (imageLoader != null) {
        Log.i(TAG, "Memory Trim Level: " + level);
        if (level < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
            if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
                imageLoader.onLowMemory(0.75f);
            } else if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW) {
                imageLoader.onLowMemory(0.50f);
            } else if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE) {
                imageLoader.onLowMemory(0.25f);
            }
        } else if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
            imageLoader.onLowMemory(0.25f);
        }
    }
}
 
Example 4
Source File: MemoryMonitorAndroid.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Register ComponentCallbacks2 to receive memory pressure signals.
 *
 */
@CalledByNative
private static void registerComponentCallbacks() {
    sCallbacks = new ComponentCallbacks2() {
            @Override
            public void onTrimMemory(int level) {
                if (level != ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
                    nativeOnTrimMemory(level);
                }
            }
            @Override
            public void onLowMemory() {
                // Don't support old onLowMemory().
            }
            @Override
            public void onConfigurationChanged(Configuration config) {
            }
        };
    ContextUtils.getApplicationContext().registerComponentCallbacks(sCallbacks);
}
 
Example 5
Source File: DownloadService.java    From Popeens-DSub with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onTrimMemory(int level) {
	ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(this);
	if(imageLoader != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
		Log.i(TAG, "Memory Trim Level: " + level);
		if (level < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
			if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
				imageLoader.onLowMemory(0.75f);
			} else if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW) {
				imageLoader.onLowMemory(0.50f);
			} else if(level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE) {
				imageLoader.onLowMemory(0.25f);
			}
		} else if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
			imageLoader.onLowMemory(0.25f);
		} else if(level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
			imageLoader.onLowMemory(0.75f);
		}
	}
}
 
Example 6
Source File: SketchUtils.java    From sketch with Apache License 2.0 6 votes vote down vote up
/**
 * 获取修剪级别的名称
 */
@NonNull
public static String getTrimLevelName(int level) {
    switch (level) {
        case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
            return "COMPLETE";
        case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
            return "MODERATE";
        case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
            return "BACKGROUND";
        case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
            return "UI_HIDDEN";
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:
            return "RUNNING_CRITICAL";
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
            return "RUNNING_LOW";
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
            return "RUNNING_MODERATE";
        default:
            return "UNKNOWN";
    }
}
 
Example 7
Source File: App.java    From Yuan-WanAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrimMemory(int level) {
    super.onTrimMemory(level);
    //当应用所有UI隐藏时应该释放UI上所有占用的资源
    if(ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN == level)
        GlideApp.get(this).clearMemory();
    //根据level级别来清除一些图片缓存
    GlideApp.get(this).onTrimMemory(level);
}
 
Example 8
Source File: App.java    From WanAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrimMemory(int level) {
    super.onTrimMemory(level);
    //当应用所有UI隐藏时应该释放UI上所有占用的资源
    if(ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN == level)
        GlideApp.get(this).clearMemory();
    //根据level级别来清除一些图片缓存
    GlideApp.get(this).onTrimMemory(level);
}
 
Example 9
Source File: Launcher.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onTrimMemory(int level) {
    super.onTrimMemory(level);
    if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
        // The widget preview db can result in holding onto over
        // 3MB of memory for caching which isn't necessary.
        SQLiteDatabase.releaseMemory();

        // This clears all widget bitmaps from the widget tray
        // TODO(hyunyoungs)
    }
    if (mLauncherCallbacks != null) {
        mLauncherCallbacks.onTrimMemory(level);
    }
}
 
Example 10
Source File: ActivityManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
int runSendTrimMemory(PrintWriter pw) throws RemoteException {
    int userId = UserHandle.USER_CURRENT;
    String opt;
    while ((opt = getNextOption()) != null) {
        if (opt.equals("--user")) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
            if (userId == UserHandle.USER_ALL) {
                getErrPrintWriter().println("Error: Can't use user 'all'");
                return -1;
            }
        } else {
            getErrPrintWriter().println("Error: Unknown option: " + opt);
            return -1;
        }
    }

    String proc = getNextArgRequired();
    String levelArg = getNextArgRequired();
    int level;
    switch (levelArg) {
        case "HIDDEN":
            level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
            break;
        case "RUNNING_MODERATE":
            level = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE;
            break;
        case "BACKGROUND":
            level = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
            break;
        case "RUNNING_LOW":
            level = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
            break;
        case "MODERATE":
            level = ComponentCallbacks2.TRIM_MEMORY_MODERATE;
            break;
        case "RUNNING_CRITICAL":
            level = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
            break;
        case "COMPLETE":
            level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
            break;
        default:
            try {
                level = Integer.parseInt(levelArg);
            } catch (NumberFormatException e) {
                getErrPrintWriter().println("Error: Unknown level option: " + levelArg);
                return -1;
            }
    }
    if (!mInterface.setProcessMemoryTrimLevel(proc, userId, level)) {
        getErrPrintWriter().println("Unknown error: failed to set trim level");
        return -1;
    }
    return 0;
}
 
Example 11
Source File: MainApplication.java    From NFC-EMV-Reader with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onTrimMemory(int level) {
    super.onTrimMemory(level);
    LogUtil.d(TAG, "\"" + TAG + "\": Application trim memory");

    switch (level) {
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
            LogUtil.d(TAG, "Trim memory: Running moderate");

            break;
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
            LogUtil.d(TAG, "Trim memory: Running low");

            break;
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:
            LogUtil.d(TAG, "Trim memory: Running critical");

            break;

        case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
            LogUtil.d(TAG, "Trim memory: UI hidden");

            break;

        case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
            LogUtil.d(TAG, "Trim memory: Background");

            break;
        case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
            LogUtil.d(TAG, "Trim memory: Moderate");

            break;
        case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
            LogUtil.d(TAG, "Trim memory: Complete");

            if (sPackageName == null) {
                sPackageName = getPackageName();
            }

            if (sFilesDirMemory == null) {
                sFilesDirMemory = getFilesDir();
            }
            if (sFilesDirPathMemory == null) {
                sFilesDirPathMemory = getFilesDir().getPath();
            }

            if (sCacheDirMemory == null) {
                sCacheDirMemory = getCacheDir();
            }
            if (sCacheDirPathMemory == null) {
                sCacheDirPathMemory = getCacheDir().getPath();
            }

            break;
    }
}
 
Example 12
Source File: AppClass.java    From intra42 with Apache License 2.0 4 votes vote down vote up
/**
 * Release memory when the UI becomes hidden or when system resources become low.
 *
 * @param level the memory-related event that was raised.
 */
public void onTrimMemory(int level) {
    super.onTrimMemory(level);

    // Determine which lifecycle or system event was raised.
    switch (level) {

        case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:

            /*
               Release any UI objects that currently hold memory.

               "release your UI resources" is actually about things like caches.
               You usually don't have to worry about managing views or UI components because the OS
               already does that, and that's why there are all those callbacks for creating, starting,
               pausing, stopping and destroying an activity.
               The user interface has moved to the background.
            */

            break;

        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:

            picassoCache.clear();

            /*
               Release any memory that your app doesn't need to run.

               The device is running low on memory while the app is running.
               The event raised indicates the severity of the memory-related event.
               If the event is TRIM_MEMORY_RUNNING_CRITICAL, then the system will
               begin killing background processes.
            */

            break;

        case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
        case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
        case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:

            picassoCache.clear();

            /*
               Release as much memory as the process can.
               The app is on the LRU list and the system is running low on memory.
               The event raised indicates where the app sits within the LRU list.
               If the event is TRIM_MEMORY_COMPLETE, the process will be one of
               the first to be terminated.
            */


            break;

        default:
            /*
              Release any non-critical data structures.

              The app received an unrecognized memory level value
              from the system. Treat this as a generic low-memory message.
            */
            break;
    }
}