1

Problem

I am trying to create a share intent chooser page [BottomSheetFragment] for sharing text in my app but i am unable to sort the apps according to the users most frequently used apps, the relevant app list is coming in random order. I want to implement it without taking any permission from the user.

From the photo we can see that Google photos is not using default Share Intent. Many other apps like Youtube are also doing the same but i couldn't find the code to sort the apps based on usage without permission.

Share text page in My App - WhatsApp should come on the first position according to usage.

Share photo page in Google Photos App

Code I tried had the following problems

  1. Only Alphabetical sorting was available using the below code.

    PackageManager pm = context.getPackageManager();
    
    Intent mainIntent = new Intent(Intent.ACTION_SEND, null);
    
    mainIntent.setType("text/plain");
    
    List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, PackageManager.MATCH_DEFAULT_ONLY); 
    
    Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));

  1. I was able to do it by taking "android.permission.PACKAGE_USAGE_STATS" permission from the user. But i can't take this permission in my production app just for sharing a text.

import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.UserHandle;
import android.util.Log;


import java.text.Collator;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class AppUsageUtil {
	private static final String TAG = "AppUsageUtil";

	/**
	 * Method
	 *
	 * @param apkPackageName

	 * @return Runtime.
	 */
	public static long  getAppRuntime(String apkPackageName) {
		long timeInForeground = 0;
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.MONTH, -1);
			long start = calendar.getTimeInMillis();
			long end = System.currentTimeMillis();
			Log.d(TAG, "start: "+ start);
			Log.d(TAG, "end: "+ end);

			UsageStatsManager usageStatsManager = (UsageStatsManager) BaseApplication.Companion.getContext().getSystemService(Context.USAGE_STATS_SERVICE);
			Map<String, UsageStats> usageStats = usageStatsManager.queryAndAggregateUsageStats(start, end);
			Log.d(TAG, "usageStats: "+ usageStats);
			timeInForeground = usageStats.get(apkPackageName).getTotalTimeInForeground();

		} catch (Exception e){
			Log.d(TAG, "getAppRuntime: " + e.toString());
		}

		return timeInForeground;
	}

	public static class AppUsageNameComparator implements Comparator<ResolveInfo> {
		public AppUsageNameComparator(List<ResolveInfo> resolveInfos) {
			mCollator.setStrength(Collator.PRIMARY);
		}

		public final int compare(ResolveInfo resolveInfoA, ResolveInfo resolveInfoB) {
			// We want to put the one targeted to another user at the end of the dialog.
			long usage1 = getAppRuntime(resolveInfoA.activityInfo.packageName);
			long usage2 = getAppRuntime(resolveInfoB.activityInfo.packageName);
			return Long.compare(usage1, usage2);
		}

		private final Collator mCollator = Collator.getInstance();
	}
}

Help will be appreciated. Thanks

Note: Someone had shared how to sort the apps alphabetically but i want to sort it based on usage. Relevant Link: Sort Android apps alphabetically?

4

0 回答 0