Advertisement
nigatigga

Untitled

Sep 8th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.22 KB | None | 0 0
  1. package com.spicycurryman.getdisciplined10.app;
  2.  
  3. import android.app.ActionBar;
  4. import android.app.ProgressDialog;
  5. import android.app.SearchManager;
  6. import android.app.SearchableInfo;
  7. import android.content.Context;
  8. import android.content.pm.ActivityInfo;
  9. import android.content.pm.ApplicationInfo;
  10. import android.content.pm.PackageInfo;
  11. import android.content.pm.PackageItemInfo;
  12. import android.content.pm.PackageManager;
  13. import android.os.AsyncTask;
  14. import android.os.Bundle;
  15. import android.support.v7.app.ActionBarActivity;
  16. import android.support.v7.widget.SearchView;
  17. import android.text.Spannable;
  18. import android.text.SpannableString;
  19. import android.view.Menu;
  20. import android.view.MenuInflater;
  21. import android.view.MenuItem;
  22. import android.view.View;
  23. import android.widget.AdapterView;
  24. import android.widget.AdapterView.OnItemClickListener;
  25. import android.widget.ListView;
  26. import android.widget.TextView;
  27.  
  28. import com.ibc.android.demo.appslist.app.ApkAdapter;
  29.  
  30. import java.util.ArrayList;
  31. import java.util.Collections;
  32. import java.util.Comparator;
  33. import java.util.List;
  34.  
  35.  
  36.  
  37. public class InstalledAppActivity extends ActionBarActivity
  38.         implements OnItemClickListener, SearchView.OnQueryTextListener {
  39.  
  40.     PackageManager packageManager;
  41.     ListView apkList;
  42.     private SearchView mSearchView;
  43.     private TextView mStatusView;
  44.  
  45.  
  46.     @Override
  47.     protected void onCreate(Bundle savedInstanceState) {
  48.  
  49.  
  50.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  51.  
  52.         super.onCreate(savedInstanceState);
  53.         setTheme(R.style.Theme_Light_appalled);
  54.  
  55.         SpannableString s = new SpannableString("Installed Apps");
  56.         s.setSpan(new TypefaceSpan(this, "miso-bold.otf"), 0, s.length(),
  57.                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  58.  
  59. // Update the action bar title with the TypefaceSpan instance
  60.         ActionBar actionBar = getActionBar();
  61.         actionBar.setTitle(s);
  62.         getActionBar().setDisplayHomeAsUpEnabled(true);
  63.  
  64.         actionBar.setHomeButtonEnabled(true);
  65.  
  66.         setContentView(R.layout.user_installed);
  67.  
  68. // Update the action bar title with the TypefaceSpan instance
  69.  
  70.         packageManager = InstalledAppActivity.this.getPackageManager();
  71.  
  72.  
  73.         /*To filter out System apps*/
  74.  
  75.         apkList = (ListView) findViewById(R.id.applist);
  76.  
  77.         new LoadApplications(InstalledAppActivity.this.getApplicationContext()).execute();
  78.  
  79.     }
  80.  
  81.  
  82.  
  83.     @Override
  84.     public boolean onCreateOptionsMenu(Menu menu) {
  85.         MenuInflater inflater = getMenuInflater();
  86.  
  87.         inflater.inflate(R.menu.block, menu);
  88.  
  89.         MenuItem searchItem = menu.findItem(R.id.action_search);
  90.  
  91.         mSearchView = (SearchView) searchItem.getActionView();
  92.  
  93.         setupSearchView(searchItem);
  94.  
  95.         return true;
  96.  
  97.     }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.     @Override
  104.     public boolean onPrepareOptionsMenu(Menu menu) {
  105.         return super.onPrepareOptionsMenu(menu);
  106.  
  107.     }
  108.  
  109.  
  110.  
  111.  
  112.     @Override
  113.     public boolean onOptionsItemSelected(MenuItem item)
  114.     {
  115.         switch (item.getItemId())
  116.         {
  117.             case android.R.id.home:
  118.                 onBackPressed();
  119.                 break;
  120.  
  121.             default:
  122.                 return super.onOptionsItemSelected(item);
  123.         }
  124.         return true;
  125.     }
  126.     private void setupSearchView(MenuItem searchItem) {
  127.  
  128.         if (isAlwaysExpanded()) {
  129.             mSearchView.setIconifiedByDefault(false);
  130.         } else {
  131.             searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
  132.                     | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
  133.         }
  134.  
  135.         SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  136.         if (searchManager != null) {
  137.             List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();
  138.  
  139.             // Try to use the "applications" global search provider
  140.             SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
  141.             for (SearchableInfo inf : searchables) {
  142.                 if (inf.getSuggestAuthority() != null
  143.                         && inf.getSuggestAuthority().startsWith("applications")) {
  144.                     info = inf;
  145.                 }
  146.             }
  147.             mSearchView.setSearchableInfo(info);
  148.         }
  149.  
  150.         mSearchView.setOnQueryTextListener(this);
  151.     }
  152.  
  153.  
  154.  
  155.     @Override
  156.     public boolean onQueryTextSubmit(String s) {
  157.         return false;
  158.     }
  159.  
  160.     @Override
  161.     public boolean onQueryTextChange(String s) {
  162.         return false;
  163.     }
  164.  
  165.  
  166.     public boolean onClose() {
  167.         mStatusView.setText("Closed!");
  168.         return false;
  169.     }
  170.  
  171.     protected boolean isAlwaysExpanded() {
  172.         return false;
  173.     }
  174.  
  175.     /**
  176.      * Return whether the given PackageInfo represents a system package or not.
  177.      * User-installed packages (Market or otherwise) should not be denoted as
  178.      * system packages.
  179.      *
  180.      * @param pkgInfo
  181.      * @return boolean
  182.      */
  183.     private boolean isSystemPackage(PackageInfo pkgInfo) {
  184.         return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
  185.                 : false;
  186.     }
  187.  
  188.     private boolean isSystemPackage1(PackageInfo pkgInfo) {
  189.         return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) ? false
  190.                 : true;
  191.     }
  192.  
  193.     private boolean isSystemPackage2(PackageInfo pkgInfo) {
  194.         return ((pkgInfo.packageName.contains("com.spicycurryman.getdisciplined10.app"))) ? false
  195.                 : true;
  196.     }
  197.  
  198.     private boolean isSystemPackage3(PackageInfo pkgInfo) {
  199.         return ((pkgInfo.packageName.contains("com.android.mms"))) ? true
  200.                 : false;
  201.     }
  202.  
  203.     private boolean isSystemPackage4(PackageInfo pkgInfo) {
  204.         return ((pkgInfo.packageName.contains("com.android.email"))) ? true
  205.                 : false;
  206.     }
  207.  
  208.  
  209.  
  210.  
  211.  
  212. // Don't need in Fragment
  213. /*@Override
  214. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  215.     inflater.inflate(R.menu.block, menu);
  216.    // super.onCreateOptionsMenu(menu,inflater);
  217. }*/
  218.  
  219.     @Override
  220.     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  221.         //ApkAdapter apkAdapter=(ApkAdapter)apkList.getAdapter();
  222.  
  223.     }
  224.  
  225.     private class LoadApplications extends AsyncTask<Void, Void, Void> {
  226.  
  227.  
  228.         private ProgressDialog pDialog;
  229.         List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();
  230.  
  231.         public LoadApplications(Context context){
  232.             Context mContext = context;
  233.         }
  234.  
  235.         @Override
  236.         protected Void doInBackground(Void... params) {
  237.  
  238.             List<PackageInfo> packageList = packageManager
  239.                     .getInstalledPackages(PackageManager.GET_PERMISSIONS);
  240.  
  241.             List<PackageInfo> packageList2 = packageManager
  242.                     .getInstalledPackages(PackageManager.GET_PERMISSIONS);
  243.  
  244.             for(PackageInfo pi : packageList) {
  245.  
  246.                 boolean b = isSystemPackage(pi);
  247.                 boolean c = isSystemPackage1(pi);
  248.                 boolean d = isSystemPackage2(pi);
  249.  
  250.  
  251.                 if ((!b || !c ) && d ){
  252.                     packageList1.add(pi);
  253.                 }
  254.  
  255.             }
  256.  
  257.             //here you got email and message apps in the
  258.  
  259.             for(PackageInfo pi : packageList) {
  260.  
  261.                 boolean b = isSystemPackage3(pi);
  262.                 boolean c = isSystemPackage4(pi);
  263.  
  264.  
  265.                 if (b || c){
  266.                     packageList1.add(pi);
  267.                 }
  268.  
  269.             }
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.             //sort by application name
  277.  
  278.             final PackageItemInfo.DisplayNameComparator comparator = new PackageItemInfo.DisplayNameComparator(packageManager);
  279.  
  280.             Collections.sort(packageList1, new Comparator<PackageInfo>() {
  281.                 @Override
  282.                 public int compare(PackageInfo lhs, PackageInfo rhs) {
  283.                     return comparator.compare(lhs.applicationInfo, rhs.applicationInfo);
  284.                 }
  285.             });
  286.  
  287.             return null;
  288.         }
  289.         @Override
  290.         protected void onCancelled() {
  291.             super.onCancelled();
  292.         }
  293.  
  294.         @Override
  295.         protected void onPreExecute() {
  296.             pDialog = new ProgressDialog(InstalledAppActivity.this);
  297.             pDialog.setMessage("Loading your apps...");
  298.             pDialog.show();
  299.  
  300.         }
  301.         //Inefficient patch to prevent Window Manager error
  302.  
  303.  
  304.         @Override
  305.         protected void onPostExecute(Void result) {
  306.  
  307.             apkList.setAdapter(new ApkAdapter(InstalledAppActivity.this, packageList1, packageManager));
  308.  
  309.             try {
  310.                 if ((this.pDialog != null) && this.pDialog.isShowing()) {
  311.                     this.pDialog.dismiss();
  312.                 }
  313.             } catch (final IllegalArgumentException e) {
  314.                 // Handle or log or ignore
  315.             } catch (final Exception e) {
  316.                 // Handle or log or ignore
  317.             } finally {
  318.                 this.pDialog = null;
  319.             }
  320.  
  321.  
  322.             super.onPostExecute(result);
  323.         }
  324.  
  325.         @Override
  326.         protected void onProgressUpdate(Void... values) {
  327.             super.onProgressUpdate(values);
  328.         }
  329.     }
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement