Advertisement
tiwiz

SearchInstalledProvidersService

Aug 6th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.71 KB | None | 0 0
  1. package it.tiwiz.whatsong;
  2.  
  3. import android.app.IntentService;
  4. import android.content.Intent;
  5. import android.content.pm.PackageManager;
  6. import android.support.v4.content.LocalBroadcastManager;
  7.  
  8. import java.util.ArrayList;
  9.  
  10. import it.tiwiz.whatsong.utils.IntentUtils;
  11. import it.tiwiz.whatsong.utils.PackageData;
  12.  
  13.  
  14. /**
  15.  * An {@link IntentService} subclass for handling asynchronous task requests in
  16.  * a service on a separate handler thread.
  17.  * <p/>
  18.  * TODO: Customize class - update intent actions and extra parameters.
  19.  */
  20. public class SearchInstalledProvidersService extends IntentService {
  21.  
  22.     private static final String TAG = SearchInstalledProvidersService.class.getSimpleName();
  23.     public static final String SEARCH_PROVIDERS_RESPONSE = TAG + ".SearchProvidersResponse";
  24.     public static final String SEARCH_INSTALLED_PROVIDERS = TAG + ".SearchInstalledProviders";
  25.     public static final String SEARCH_NOT_INSTALLED_PROVIDERS = TAG + ".SearchNotInstalledProviders";
  26.  
  27.     public static final String SEARCH_PROVIDERS_KEY = "SearchProvidersKey";
  28.  
  29.     private final String[] allProvidersPackages, allProvidersNames;
  30.     private boolean showInstalledPackages;
  31.     private PackageManager packageManager;
  32.  
  33.     {
  34.         allProvidersPackages = WhatSongApp.getStringArrayBy(R.array.softwares_packages);
  35.         allProvidersNames = WhatSongApp.getStringArrayBy(R.array.softwares_names);
  36.     }
  37.  
  38.     public SearchInstalledProvidersService() {
  39.         super("SearchInstalledProvidersService");
  40.     }
  41.  
  42.     @Override
  43.     protected void onHandleIntent(Intent intent) {
  44.         if (intent != null) {
  45.             final String action = intent.getAction();
  46.             if (isPackagesListRequest(action)) {
  47.                 handlePackageRequest(action);
  48.             }
  49.         }
  50.     }
  51.  
  52.     /**
  53.      * This method will check if the given Action is a request for installed or
  54.      * not installed packages
  55.      */
  56.     protected boolean isPackagesListRequest(String action) {
  57.         return (action.equals(SEARCH_INSTALLED_PROVIDERS) ||
  58.                 action.equals(SEARCH_NOT_INSTALLED_PROVIDERS));
  59.     }
  60.  
  61.     /**
  62.      * This method is the skeleton that must be called when dealing with requests for
  63.      * installed and not installed packages
  64.      */
  65.     protected void handlePackageRequest(String action) {
  66.  
  67.         showInstalledPackages = isInstalledPackagesListRequest(action);
  68.         ArrayList<PackageData> resultList = getPackagesBasedOnStatus();
  69.         PackageData[] result = getArrayFrom(resultList);
  70.         sendResponse(result);
  71.     }
  72.  
  73.     /**
  74.      * This method will check if the given action is a call for installed
  75.      * packages
  76.      */
  77.     protected boolean isInstalledPackagesListRequest(String action) {
  78.         return (action.equals(SEARCH_INSTALLED_PROVIDERS));
  79.     }
  80.  
  81.     /**
  82.      * This method will retrieve every package that has been requested.
  83.      * <b>Note</b>: this logic has been written to work with both installed
  84.      * and not installed packages filter.
  85.      */
  86.     protected ArrayList<PackageData> getPackagesBasedOnStatus() {
  87.  
  88.         ArrayList<PackageData> packages = new ArrayList<>();
  89.  
  90.         packageManager = getPackageManager();
  91.         String currentPackage;
  92.  
  93.         for(int i = 0; i < allProvidersPackages.length; i++){
  94.             currentPackage = allProvidersPackages[i];
  95.             if (shouldAddPackage(currentPackage)) {
  96.                 packages.add(new PackageData(currentPackage, allProvidersNames[i]));
  97.             }
  98.         }
  99.  
  100.         return packages;
  101.     }
  102.  
  103.     /**
  104.      * This method decides if the given package name shall be added to the list using the following rules
  105.      * <b>Add if</b>
  106.      * <ul>
  107.      *     <li>if the package is installed and only the installed packages are requested</li>
  108.      *     <li>if the package is <b>not</b> installed and only the not installed packages are requested</li>
  109.      * </ul>
  110.      *
  111.      * <b>Note</b>: given the two conditions, we can easily see that the conditions can be reduced to
  112.      * <ol>
  113.      *     <li>{@code isPackageInstalled && showInstalledOnly}</li>
  114.      *     <li>{@code !isPackageInstalled && !showInstalledOnly}</li>
  115.      * </ol>
  116.      * from these 2 conditions, we can see that the package should be added only if both the conditions
  117.      * are verified or if both are <b>not</b> verified so the package is added only if the two conditions
  118.      * are equal.
  119.      */
  120.     protected boolean shouldAddPackage(String packageName) {
  121.         return (isPackageInstalled(packageName) == showInstalledPackages);
  122.     }
  123.  
  124.     /**
  125.      * This method checks if the given package is installed in the system
  126.      */
  127.     protected boolean isPackageInstalled(String packageName) {
  128.         boolean isInstalled = true;
  129.         try {
  130.             packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
  131.         } catch (PackageManager.NameNotFoundException e) {
  132.             isInstalled = false;
  133.         }
  134.         return isInstalled;
  135.     }
  136.  
  137.     protected PackageData[] getArrayFrom(ArrayList<PackageData> data) {
  138.         final int resultSize = data.size();
  139.         PackageData[] result = new PackageData[resultSize];
  140.  
  141.         for(int i = 0; i < resultSize; i++) {
  142.             result[i] = data.get(i);
  143.         }
  144.  
  145.         return result;
  146.     }
  147.  
  148.     /**
  149.      * This method sends back the list of {@link it.tiwiz.whatsong.utils.PackageData} containing
  150.      * the requested packages using a {@link android.support.v4.content.LocalBroadcastManager}
  151.      */
  152.     protected void sendResponse(PackageData[] packageData) {
  153.         final Intent responseIntent = IntentUtils.getSendInstalledProvidersResponse(packageData);
  154.         LocalBroadcastManager.getInstance(this).sendBroadcast(responseIntent);
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement