Mobile Phone Management application Research "2"--Garbage cleanup

Source: Internet
Author: User
Tags root access

Welcome reprint, Reprint please specify: HTTP://BLOG.CSDN.NET/ZHGXHUAA


Description

In the general article mentioned in the garbage cleanup, this article will focus on the cache, uninstall residue, useless data, such as "static content" cleanup, there is about the system process cleanup and mobile phone acceleration related functions, will be put in the "mobile phone acceleration" in the introduction of research.


What is garbage?
On the definition of system garbage
Now many mobile phone management software has the function of garbage cleanup, this article also to do a simple analysis and introduction of garbage cleanup. Before the formal analysis, let's first look at "What is System garbage?" ”。 In the Baidu undergraduate, about "system garbage" is the definition of: system garbage, is the system is no longer required files collectively. When you browse the webpage, install and uninstall the program residue files and registry key values. These are files that have no effect on the system and can only add a burden to the system, so it is called garbage.

About system garbage in fact, "All of the resources we no longer need, files, caches, is rubbish." We clean up the system garbage to clean up the files that are no longer needed.


What can be cleaned up in Android


First look at the current popular mobile phone management software Total, will those as can clean up the garbage, below is the use of the higher rate of four mobile phone management software garbage cleanup part:




Combined with these mobile phone management software, the trash that can be cleaned up on Android phones is summarized as follows:

A. Applying A cache file

B. Applying uninstall residue

C. Useless installation package

D. Memory data

E. System garbage (logs, thumbnails, empty folders, etc.)

F. Advertising files

G. Large files (files with file sizes larger than a certain value)

H. Useless files on SD card

In the cleanup of these junk files, some need root privileges, and some do not need, the next will be a unified analysis of the introduction.


Android app Data management strategy


After understanding the definition of junk files, here are some of the management strategies for app data in Android:

Data storage in Android can be easily divided into internal storage (mainly refers to the application Data directory) and external storage (mainly SD card) two cases. Android does not have the same management strategy for these two different situations. The data in the built-in storage is stored in the principle of process isolation, while external storage is stored in the principle of global sharing.

It is generally necessary to clean up the application private data in the built-in storage, which generally does not require special permissions to clean up data in external storage.


Quick cleanup and deep cleanup


In many mobile phone management software will find a "quick cleanup" and "deep clean", here is a brief:

    • The so-called rapid cleanup, in fact, is often referred to in the garbage scanning only two to three levels of directory, temporarily do not need to do some deep traversal and other time-consuming operation.
    • The so-called deep cleaning, often refers to all the garbage can be cleaned up a comprehensive scan, the directory is deeply traversed.

Here is one thing to say is that the two seemingly distinct, but actually can be integrated with each other, a feasible measurement strategy is:

After the management software starts, select the System idle time, do the deep scan regularly, do a quick scan when the user triggers manually. But here comes another problem: scanning in idle time to have the right strategy, when to start, when to pause, you must ensure that the application is not affected by the normal use of the premise.


System Garbage Cleanup


Apply Cache cleanup


For the application of cache cleanup, this article will introduce two implementation scenarios, namely: to implement the application cache cleanup and use the system interface to implement the application cache cleanup, the next will be described separately.


Implement app Cache Cleanup yourself


To clean up the app cache, first understand how the app's cache is stored. Generally speaking, the cache applied in Android includes the following sections:

A./data/data/ package_name /cache (application cache)

B./mnt/sdcard/android/ package_name /cache (external application cache, Froyo support later)

C./data/data/package_name/database/webview.db*(WebView cache)

D./data/data/package_name/database/webviewcache.db*(WebView cache)

E. Some other/data/data/ package_name /*cache directory (app cache)

F./data/data/package_name/files (can also choose to clean up when more stringent cleanup strategies are available)

For files stored in the/data/data/ package_name directory, the app itself and the app with which it shared the UserID have full read and write execution permissions, and other applications do not have read and write permissions, as follows:




From the above analysis, we draw the first solution to clean up the application cache:

With root access, traverse the cache directory of the scanned app and delete it if found. The following two paragraphs are some sample code that calculates the cache size and cleans up the cache:

Code snippet to calculate the Cache size section:



To clean up the cached part of the code snippet:




Of course, the scanning and cleanup work here can also be done by JNI in C code, and the implementation is similar. You can see the Free_cache function in @frameworks/native/cmds/installd/commands.c if the C code is cached.


Application cache cleanup with system interface


Friends who have used Android phones believe that the "Erase cache" feature is in the app details page of the phone's "settings--and apps", which should be on my personal phone:




OK, here, the question is, can we use the system interface to implement the "clear cache" function? The app details page in Android is located in @/packages/apps/settings/src/com/android/settings/applications/installedappdetails.java.

The code for cleaning the application Cache section in Installedappdetails is as follows:




The implementation of Deleteapplicationcachefiles in Packagemanager is as follows:




Here are a few points to note:

A. The Deleteapplicationcachefiles interface is hide and cannot be called directly in the code.

B. You need to declare Delete_cache_files permissions using the Deleteapplicationcachefiles interface.

C. The system application cache cannot be purged through the Deleteapplicationcachefiles interface.

D. The cache cleanup result completes the callback via the Ipackagedataobserver interface.

In addition to the Deleteapplicationcachefiles interface, there are two interfaces in the Packagemanager that can optionally be called:




Here is some sample code that uses the second cache cleanup method, as follows:






Here is some sample code that gets the size of each app cache:








Comparison of two implementation schemes


After introducing the two application cache cleanup methods, here is a simple comparison to choose from when using:

A. From the scope: the first way to clean up the scope is broader and more thorough. The second way can only clean up non-system applications, the first way to clean up all apps, and even apply custom cache files.

B. From permissions: The first method requires root permission, the second method only needs to specify Delete_cache_files in Androidmanifest.

C. From security: Use the first approach when you have the right strategy to avoid cleaning up the cache that should not be cleaned out.

In summary, the first way of cleaning up a broader and more thorough, in the case of root access, it is recommended to use the first way.


Apply Uninstall Residue Cleanup


As described in 1.3, application-installed data is stored in both internal and external storage methods. The data in the corresponding built-in storage is deleted automatically when the app is deleted, so you don't have to worry about it. However, many Android applications create their own application-specific directories on the mobile terminal's memory card for storing cached files, temporary files, or media files such as video, audio, and books that are downloaded over the network. However, when the Android application is uninstalled, the files that remain on the memory card are not deleted, and most of the files are useless to the user and belong to the junk file. If the user installs and uninstalls the new software frequently, the memory card will produce a large number of application residue files, not only occupy the memory card space, but also make the memory card file management becomes difficult. Therefore, it is necessary to realize the identification and cleanup of the residual directory of Android application.

One of the key points in applying uninstall residue cleanup is to apply the identification of residual directories. Here are two possible scenarios:


Scenario One: Maintaining file mapping information


In a file or database application package name (unique) and the file on the SD card mapping information, you can use the application package name key, because the package name of different applications is unique, will not be duplicated; With the file on the SD card as value, the mapping is as follows:

Key

Value

App package name (unique)

Folder

When the app uninstall event is detected, the user is prompted to delete the file if there are any corresponding files. The following are some of the issues that are required when using this scenario:

A. Mapping table creation: Mapping tables need to be established for applications with high frequency and user volume. It is best to have background scanning application set up, and then sent to the client, or the client to report the information to the background, the background correction. The establishment of a mapping table is the key to recognition.

B. Error identification problem: In general, data in different applications is placed in different folders and there is no duplication. But in practice, there will inevitably be repeated situations. There are two ways to handle this scenario:

1) The mapping relationship refinement to the file, delete the file and then determine whether the upper folder is empty, empty is deleted.

2) There are multiple applications where the mapping relationship repeats when it is judged that all apps have been uninstalled before deleting the folder.


Scenario Two: Documenting the application installation process


The universality of the scheme is not as strong as that of the first one, but it can also be used as a reference for implementing the scheme. The scheme is as follows:

1) When installing through the management software, the files generated by the application are installed through log records. When the application is running, monitor the changes of the SD file, and when the changes are found, the current application (the top of the runtime stack) is the creator of the file, and the corresponding relationship is recorded in log.

2) The reverse process is performed based on the log information when uninstalling the application through the management software.


Useless installation package cleanup


Cleanup of useless installation packages is relatively straightforward. The criteria for judging a useless installation package are:

A. The apk file exists in the storage directory, but the APK has been installed

B. The apk file is corrupted

There are two ways to handle a scan installation package:

A. Deep Scan: Scan all directories on the SD card

B. Fast scan, only scan the phone management software (Pea pod, 360 mobile phone helper, app Bao, etc.) and browser (UCWEB, QQ browser) and download directory.

You can also follow this idea to clean up other download files.


System Garbage Cleanup


System garbage cleanup includes temporary files, thumbnails, system logs, invalid files, blank files and other cleanup, below to do a brief introduction.


Clean up the System log


Log files are divided into the system log and the application log, respectively, their respective storage locations are:

1) The system logs are stored in the following locations (different phones may vary):

A./data/local/tmp/*

B./data/tmp/*

C./data/system/usagestats/*

D./data/system/appusagestates/*

E./data/system/dropbox/*

F./data/tombstones/*

G./data/anr/*

H./dev/log/main

2) Application log storage location can be judged: SD card suffix named ". Log" or "*log.txt" and other end of the file.


Clean up thumbnail image


There is a hidden directory under the DICM directory on the SD card, called ". Thumbnails", which holds the cache of the system picture. Cleaning up the cache is mostly about cleaning up the directory. The application directory may also have thumbnail files, but it is not easy to identify, so it is not recommended to clean up, you can clean up the residual data together.


Clean up invalid files and blank files


Judging criteria:

A. A file with a length of 0 is considered a blank file and can be deleted

B. Folders that do not contain any files or folders are considered blank folders and can be deleted.

C. In addition to the above two cases, you can also scan the creation time of a file, and a file that is not used for a long time is considered invalid.



Large file cleanup


For large file recognition is relatively simple, only to determine whether the file size exceeds a certain threshold (for example: Pea pod is considered larger than 10M size file) can be. But here are two points to note:

A. Large files are generally video files or application data (such as Baidu map data), for these files in the cleanup is recommended to default "unchecked" status.

B. You can make full use of the mapping relationships established in 2.2.1 to provide more accurate recommendations on whether large files are recommended for deletion.

For the identification and processing of advertising files and so on here no longer repeat, the principle is similar to the above.



Summarize


The method in this article can basically realize the comprehensive cleanup of the junk files in the Android system. The emphasis here is:

A. Garbage cleaning is a double-edged sword, must have the appropriate strategy, otherwise it will affect the user experience.

B. Be sure to take this attitude when cleaning up junk files: It's better to be wrong than to be.

Finally, I hope this article can give all the friends who are interested in garbage cleanup and cell phone management a little help, will be honored. Have any questions to welcome the exchange and discussion. The next article will introduce "mobile phone acceleration"



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.