Advertisement
nigatigga

installedappactivity 910

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