Recently, when doing things, we need to get the size of the application software and see how many people write it. It's much the same. Today, I will refer to other people's code and add some of my own understanding. Tidy up and share with you.
Here are some examples of AIDL related methods for obtaining application sizes. There are also many examples on this website.
Scenario of application: To collect application-related information of multiple apps, including application size, and then report information together with other software information (package name, application name, application icon, etc.).
This involves a question: how to ensure that other information collected from multiple applications is uploaded together after completion? Because the above method of obtaining size can only get the size of one application at a time, and the method and application size of obtaining other information of application belong to asynchronous operation. Software size is obtained separately by the above methods, and other information is obtained by other methods. More than one software information should be acquired and uploaded.
Solution: Use callbacks and Count Down Latch to solve the above scenario.
1.AIDL file
According to the above structure, add two files, IPackageStatsObserver.aidl and packageStats.aidl, in the src/main/aidl/android.content.pm/directory.
The following are the contents of these two documents:
IPackageStatsObserver.aidl
package android.content.pm;
import android.content.pm.PackageStats;
oneway interface IPackageStatsObserver {
void onGetStatsCompleted(in PackageStats pStats, boolean succeeded);
}
packageStats.aidl
package android.content.pm;
parcelable PackageStats;
2.size size acquisition
public void queryPackageSize(CountDownLatch latch,IQueryPkgSizeCallback callBack, Context context, String pkgName) throws Exception{
if ( pkgName != null){
//Getting the hidden function getPackageSizeInfo of PackageManager class by using radiation mechanism
PackageManager pm = context.getPackageManager(); //Get the pm object
try {
//Obtaining the Hidden Function by Reflection Mechanism
Method getPackageSizeInfo = pm.getClass().getMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class);
//Call the function and assign parameters to it. When the calling process is completed, the function of the PkgSizeObserver class is called back.
getPackageSizeInfo.invoke(pm, pkgName,new PkgSizeObserver(latch,callBack));
}
catch(Exception ex){
Log.e(TAG, "NoSuchMethodException") ;
ex.printStackTrace() ;
throw ex ; // throw
}
}
}
//Bindler mechanism service class formed by aidl file
public class PkgSizeObserver extends IPackageStatsObserver.Stub{
private IQueryPkgSizeCallback callback;
private CountDownLatch latch;
PkgSizeObserver(CountDownLatch latch,IQueryPkgSizeCallback callback){
this.latch = latch;
this.callback = callback;
}
/*** Callback function,
* @param pStats ,Return data is encapsulated in the PackageStats object
* @param succeeded Representatives Callback Successful
*/
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
throws RemoteException {
// TODO Auto-generated method stub
Log.i(TAG,"PkgSizeObserver succeeded="+succeeded);
if(succeeded){
cachesize = pStats.cacheSize ; //Cache size n
datasize = pStats.dataSize ; //data size
codesize = pStats.codeSize ; //Application size
totalsize = cachesize + datasize + codesize ;
Log.i(TAG, "cachesize--->"+cachesize+" datasize---->"+datasize+ " codeSize---->"+codesize);
callback.queryPkgSize(true,totalsize);
}else{
Log.i(TAG,"get size fail------>");
}
latch.countDown();//For each successful acquisition of an app value, the counter is subtracted by 1 until the counter is zero, no blocking, and subsequent tasks are performed.
}
}
3. Callback interface
public interface IQueryPkgSizeCallback{
void queryPkgSize(boolean isSucceed,long pkgTotalSize);
}
4.CountDownLatch uses
CountDownLatch latch = new CountDownLatch(list.size);//The list set of multiple app s to be requested, and the counter size is the list size
if (list != null) {
final ArrayList<Object name> apps = new ArrayList<>();//As a list that holds multiple application information, the last list returned
for(final Iterator<Object name> iterator = list.iterator(); iterator.hasNext();) {
final Object name info = iterator.next();
try {
mModule.queryPackageSize(latch,new Class name.IQueryPkgSizeCallback() {
@Override
public void queryPkgSize(boolean isSucceed, long pkgTotalSize) {
if(isSucceed){
Log.i(TAG,"getAllUserApp mAppInfoItem pkgName="+info.getAppPackage()+" system_flag="+info.getAppSystemFlag()+" app size="+mModule.formateFileSize(AEmmApplicaton.getContext(),pkgTotalSize));
info.setAppTotalSize(mModule.formateFileSize(AEmmApplicaton.getContext(),pkgTotalSize));
InstallApp.AppData app = new InstallApp.AppData(info.getAppPackage(), info.getAppName(),
info.getAppFirstInstallTime(), info.getAppVersion(), info.getAppImageToString(), info.getAppSystemFlag(), info.getAppTotalSize());
apps.add(app);//Every time a complete application information is obtained, it is stored in the list.
Log.i(TAG,"succ----->mLock.unlock()");
}else{
Log.i(TAG,"getAllUserApp queryPkgSize fail");
}
}
}, context,info.getAppPackage());
} catch (Exception e) {
e.printStackTrace();
}
}
try {
latch.await();
data = new InstallApp(apps);//When the counter is zero, perform the task here
Log.i(TAG, "----> latch.await()"+" app.size="+apps.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
As for the detailed use of Count Down Latch, I will not elaborate on it. There are many links on the Internet, as follows:
http://www.importnew.com/15731.html
The above is mainly for the actual project used in the example, combined with online articles on the acquisition of software size, made a comprehensive demonstration. If you have any questions, welcome to exchange.