thieumao

Check permision Android

May 31st, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.93 KB | None | 0 0
  1. package com.haynhanh.listapps;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.app.AlertDialog;
  7. import android.app.ListActivity;
  8. import android.app.ProgressDialog;
  9. import android.content.ActivityNotFoundException;
  10. import android.content.DialogInterface;
  11. import android.content.Intent;
  12. import android.content.pm.ApplicationInfo;
  13. import android.content.pm.PackageInfo;
  14. import android.content.pm.PackageManager;
  15. import android.content.pm.PackageManager.NameNotFoundException;
  16. import android.net.Uri;
  17. import android.os.AsyncTask;
  18. import android.os.Bundle;
  19. import android.util.Log;
  20. import android.view.Menu;
  21. import android.view.MenuInflater;
  22. import android.view.MenuItem;
  23. import android.view.View;
  24. import android.widget.ListView;
  25. import android.widget.Toast;
  26.  
  27. import android.app.ListActivity;
  28. import android.os.Bundle;
  29.  
  30. public class ListFilterActivity extends ListActivity {
  31.     private PackageManager packageManager = null;
  32.     private List<ApplicationInfo> applist = null;
  33.     private ApplicationAdapter listadaptor = null;
  34.     String strActionFilter = null;
  35.  
  36.     @Override
  37.     public void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.  
  40.         Bundle bundle = getIntent().getExtras();
  41.         strActionFilter = bundle.getString("actionFilter");
  42. //      Toast.makeText(this, ""+strActionFilter, Toast.LENGTH_SHORT).show();
  43.  
  44.         setContentView(R.layout.activity_main);
  45.  
  46.         packageManager = getPackageManager();
  47.  
  48.         new LoadApplications().execute();
  49.     }
  50.  
  51.     public boolean onCreateOptionsMenu(Menu menu) {
  52.         MenuInflater inflater = getMenuInflater();
  53.         inflater.inflate(R.menu.menu, menu);
  54.  
  55.         return true;
  56.     }
  57.  
  58.     public boolean onOptionsItemSelected(MenuItem item) {
  59.         boolean result = true;
  60.  
  61.         switch (item.getItemId()) {
  62.         case R.id.menu_about: {
  63.             displayAboutDialog();
  64.  
  65.             break;
  66.         }
  67.         case R.id.menu_search: {
  68.             searchAction();
  69.  
  70.             break;
  71.         }
  72.         default: {
  73.             result = super.onOptionsItemSelected(item);
  74.  
  75.             break;
  76.         }
  77.         }
  78.  
  79.         return result;
  80.     }
  81.  
  82.     private void displayAboutDialog() {
  83.         final AlertDialog.Builder builder = new AlertDialog.Builder(this);
  84.         builder.setTitle(getString(R.string.about_title));
  85.         builder.setMessage(getString(R.string.about_desc));
  86.  
  87.         builder.setPositiveButton("Know More",
  88.                 new DialogInterface.OnClickListener() {
  89.                     public void onClick(DialogInterface dialog, int id) {
  90.                         Intent browserIntent = new Intent(Intent.ACTION_VIEW,
  91.                                 Uri.parse("http://haynhanh.com"));
  92.                         startActivity(browserIntent);
  93.                         dialog.cancel();
  94.                     }
  95.                 });
  96.         builder.setNegativeButton("No Thanks!",
  97.                 new DialogInterface.OnClickListener() {
  98.                     public void onClick(DialogInterface dialog, int id) {
  99.                         dialog.cancel();
  100.                     }
  101.                 });
  102.  
  103.         builder.show();
  104.     }
  105.  
  106.     private void searchAction() {
  107.         Intent intent = new Intent(ListFilterActivity.this,
  108.                 SearchActivity.class);
  109.         startActivity(intent);
  110.     }
  111.  
  112.     @Override
  113.     protected void onListItemClick(ListView l, View v, int position, long id) {
  114.         super.onListItemClick(l, v, position, id);
  115.  
  116.         ApplicationInfo app = applist.get(position);
  117.         try {
  118.             Intent intent = packageManager
  119.                     .getLaunchIntentForPackage(app.packageName);
  120.  
  121.             if (null != intent) {
  122.                 startActivity(intent);
  123.             }
  124.         } catch (ActivityNotFoundException e) {
  125.             Toast.makeText(ListFilterActivity.this, e.getMessage(),
  126.                     Toast.LENGTH_LONG).show();
  127.         } catch (Exception e) {
  128.             Toast.makeText(ListFilterActivity.this, e.getMessage(),
  129.                     Toast.LENGTH_LONG).show();
  130.         }
  131.     }
  132.  
  133. //  private List<ApplicationInfo> checkForLaunchIntent(
  134. //          List<ApplicationInfo> list) {
  135. //      ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
  136. //      for (ApplicationInfo info : list) {
  137. //          try {
  138. //              if (null != packageManager
  139. //                      .getLaunchIntentForPackage(info.packageName)) {
  140. ////                    Log.d("test permission", "thieumao " + info.packageName);
  141. //                  /*
  142. //                      SEND_SMS nhanTin
  143. //                      CALL_PHONE goiDien
  144. //                      INTERNET dungMang
  145. //                      GET_ACCOUNTS layThongTin
  146. //                   */
  147. //                  int check = 0;
  148. //                  if (strActionFilter.contains("nhanTin")){
  149. //                      if (checkPermission(info, "SEND_SMS")== 1){
  150. //                          check = 1;
  151. //                      };
  152. //                  } else if (strActionFilter.contains("goiDien")){
  153. //                      if (checkPermission(info, "SEND_SMS")== 1){
  154. //                          check = 1;
  155. //                      };
  156. //                  } else if (strActionFilter.contains("dungMang")){
  157. //                      if (checkPermission(info, "SEND_SMS")== 1){
  158. //                          check = 1;
  159. //                      };                     
  160. //                  } else if (strActionFilter.contains("layThongTin")){
  161. //                      if (checkPermission(info, "SEND_SMS")== 1){
  162. //                          check = 1;
  163. //                      };
  164. //                  }
  165. //                  if (check == 0){
  166. //                      applist.add(info);
  167. //                  }
  168. //                 
  169. //              }
  170. //          } catch (Exception e) {
  171. //              e.printStackTrace();
  172. //          }
  173. //      }
  174. //
  175. //      return applist;
  176. //  }
  177.    
  178.     private List<ApplicationInfo> filterForLaunchIntent(
  179.             List<ApplicationInfo> list, String strActionFilter) {
  180.         ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
  181.         for (ApplicationInfo info : list) {
  182.             try {
  183.                 if (null != packageManager
  184.                         .getLaunchIntentForPackage(info.packageName)) {
  185. //                  Log.d("test permission", "thieumao " + info.packageName);
  186.                     /*
  187.                         SEND_SMS nhanTin
  188.                         CALL_PHONE goiDien
  189.                         INTERNET dungMang
  190.                         GET_ACCOUNTS layThongTin
  191.                      */
  192.                     int check = 0;
  193.                     if (strActionFilter.contains("nhanTin")){
  194.                         if (checkPermission(info, "SEND_SMS")== 1){
  195.                             check = 1;
  196.                         };
  197.                     } else if (strActionFilter.contains("goiDien")){
  198.                         if (checkPermission(info, "CALL_PHONE")== 1){
  199.                             check = 1;
  200.                         };
  201.                     } else if (strActionFilter.contains("dungMang")){
  202.                         if (checkPermission(info, "INTERNET")== 1){
  203.                             check = 1;
  204.                         };                     
  205.                     } else if (strActionFilter.contains("layThongTin")){
  206.                         if (checkPermission(info, "GET_ACCOUNTS")== 1){
  207.                             check = 1;
  208.                         };
  209.                     }
  210.                     if (check == 1){
  211.                         applist.add(info);
  212.                     }
  213.                    
  214.                 }
  215.             } catch (Exception e) {
  216.                 e.printStackTrace();
  217.             }
  218.         }
  219.  
  220.         return applist;
  221.     }
  222.    
  223.     private int checkPermission(ApplicationInfo applicationInfo, String strPermission){
  224.         try {
  225.             PackageInfo packageInfo = packageManager.getPackageInfo(
  226.                     applicationInfo.packageName,
  227.                     PackageManager.GET_PERMISSIONS);
  228. //          appNameAndPermissions.append(packageInfo.packageName + "*:\n");
  229.             // Get Permissions
  230.             String[] requestedPermissions = packageInfo.requestedPermissions;
  231.             if (requestedPermissions != null) {
  232.                 for (int i = 0; i < requestedPermissions.length; i++) {
  233.                     if (requestedPermissions[i].contains(strPermission)){
  234.                         return 1;
  235.                     }
  236.                     Log.d("test permission", "mao "+requestedPermissions[i]);
  237. //                  appNameAndPermissions.append(requestedPermissions[i]
  238. //                          + "\n");
  239.                 }
  240. //              appNameAndPermissions.append("\n");
  241.             }
  242.         } catch (NameNotFoundException e) {
  243.             e.printStackTrace();
  244.             return 0;
  245.         }
  246.         return 0;
  247.     }
  248.  
  249.     private class LoadApplications extends AsyncTask<Void, Void, Void> {
  250.         private ProgressDialog progress = null;
  251.  
  252.         @Override
  253.         protected Void doInBackground(Void... params) {
  254. //          applist = checkForLaunchIntent(packageManager
  255. //                  .getInstalledApplications(PackageManager.GET_META_DATA));
  256.             applist = filterForLaunchIntent(packageManager
  257.                     .getInstalledApplications(PackageManager.GET_META_DATA), strActionFilter);
  258.            
  259.             listadaptor = new ApplicationAdapter(ListFilterActivity.this,
  260.                     R.layout.snippet_list_row, applist);
  261.  
  262.             return null;
  263.         }
  264.  
  265.         @Override
  266.         protected void onCancelled() {
  267.             super.onCancelled();
  268.         }
  269.  
  270.         @Override
  271.         protected void onPostExecute(Void result) {
  272.             setListAdapter(listadaptor);
  273.             progress.dismiss();
  274.             super.onPostExecute(result);
  275.         }
  276.  
  277.         @Override
  278.         protected void onPreExecute() {
  279.             progress = ProgressDialog.show(ListFilterActivity.this, null,
  280.                     "Loading application info...");
  281.             super.onPreExecute();
  282.         }
  283.  
  284.         @Override
  285.         protected void onProgressUpdate(Void... values) {
  286.             super.onProgressUpdate(values);
  287.         }
  288.     }
  289.  
  290. }
Advertisement
Add Comment
Please, Sign In to add comment