Skip to content

All Application Usage

TheBotBox edited this page May 14, 2020 · 4 revisions

Fetch the list of all application

Once, permission has been granted, one can easily fetch the list of application used by calling,

            Monitor.scan().getAppLists(this).fetchFor(DurationRange.TODAY);

where .getAppList(UsageContracts.View mView) returns a callback in your view once the list has been fetched.

further .fetchFor() requires the duration for which you want to query for usage details.
As of now, one can choose from DurationRange.TODAY, DurationRange.YESTERDAY, DurationRange.WEEK, DurationRange.MONTH

Final Implementation of the UsageLibrary may look like below:

    public class MainActivity extends AppCompatActivity implements UsageContracts.View{
    
     @Override
    protected void onResume() {
        super.onResume();
        if (Monitor.hasUsagePermission()) {
            Monitor.scan().getAppLists(this).fetchFor(DurationRange.TODAY);
            init();
        } else {
            Monitor.requestUsagePermission();
        }
    }
    
    @Override
    public void getUsageData(List<AppData> usageData, long mTotalUsage, int duration) {
      /**
       * 
       * @param usageData list of application that has been used within the duration for which query has been made.
       * @param mTotalUsage a sum total(in millis) of the usage by each and every app with in the request duration. 
       * @param the same duration for which query has been made i.e.fetchFor(DurationRange...)
       */
    }    
    
    @Override
    public void showProgress() {
        //optional
    }

    @Override
    public void hideProgress() {
        //optional
    }
}

mTotalUsage returns total usage time in milliSeconds. To convert into string format, use library's in-built function i.e. UsageUtils.humanReadableMillis(mTotalUsage)

List<AppData> usageData list of used apps with usage stats. For further details on AppData model, please dive into AppAdapter.java

Have a look on final output: