Java Code Examples for android.content.ComponentCallbacks2#TRIM_MEMORY_RUNNING_MODERATE

The following examples show how to use android.content.ComponentCallbacks2#TRIM_MEMORY_RUNNING_MODERATE . 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: 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 2
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 3
Source File: MusicControlModule.java    From react-native-music-control with MIT License 6 votes vote down vote up
@Override
public void onTrimMemory(int level) {
    switch(level) {
        // Trims memory when it reaches a moderate level and the session is inactive
        case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
            if(session.isActive()) break;

            // Trims memory when it reaches a critical level
        case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:

            Log.w(TAG, "Control resources are being removed due to system's low memory (Level: " + level + ")");
            destroy();
            break;
    }
}
 
Example 4
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 5
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 6
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 7
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 8
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;
    }
}