Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.haynhanh.listapps;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.AlertDialog;
- import android.app.ListActivity;
- import android.app.ProgressDialog;
- import android.content.ActivityNotFoundException;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.ApplicationInfo;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.ListView;
- import android.widget.Toast;
- import android.app.ListActivity;
- import android.os.Bundle;
- public class ListFilterActivity extends ListActivity {
- private PackageManager packageManager = null;
- private List<ApplicationInfo> applist = null;
- private ApplicationAdapter listadaptor = null;
- String strActionFilter = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Bundle bundle = getIntent().getExtras();
- strActionFilter = bundle.getString("actionFilter");
- // Toast.makeText(this, ""+strActionFilter, Toast.LENGTH_SHORT).show();
- setContentView(R.layout.activity_main);
- packageManager = getPackageManager();
- new LoadApplications().execute();
- }
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.menu, menu);
- return true;
- }
- public boolean onOptionsItemSelected(MenuItem item) {
- boolean result = true;
- switch (item.getItemId()) {
- case R.id.menu_about: {
- displayAboutDialog();
- break;
- }
- case R.id.menu_search: {
- searchAction();
- break;
- }
- default: {
- result = super.onOptionsItemSelected(item);
- break;
- }
- }
- return result;
- }
- private void displayAboutDialog() {
- final AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(getString(R.string.about_title));
- builder.setMessage(getString(R.string.about_desc));
- builder.setPositiveButton("Know More",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- Intent browserIntent = new Intent(Intent.ACTION_VIEW,
- Uri.parse("http://haynhanh.com"));
- startActivity(browserIntent);
- dialog.cancel();
- }
- });
- builder.setNegativeButton("No Thanks!",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
- builder.show();
- }
- private void searchAction() {
- Intent intent = new Intent(ListFilterActivity.this,
- SearchActivity.class);
- startActivity(intent);
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id);
- ApplicationInfo app = applist.get(position);
- try {
- Intent intent = packageManager
- .getLaunchIntentForPackage(app.packageName);
- if (null != intent) {
- startActivity(intent);
- }
- } catch (ActivityNotFoundException e) {
- Toast.makeText(ListFilterActivity.this, e.getMessage(),
- Toast.LENGTH_LONG).show();
- } catch (Exception e) {
- Toast.makeText(ListFilterActivity.this, e.getMessage(),
- Toast.LENGTH_LONG).show();
- }
- }
- // private List<ApplicationInfo> checkForLaunchIntent(
- // List<ApplicationInfo> list) {
- // ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
- // for (ApplicationInfo info : list) {
- // try {
- // if (null != packageManager
- // .getLaunchIntentForPackage(info.packageName)) {
- //// Log.d("test permission", "thieumao " + info.packageName);
- // /*
- // SEND_SMS nhanTin
- // CALL_PHONE goiDien
- // INTERNET dungMang
- // GET_ACCOUNTS layThongTin
- // */
- // int check = 0;
- // if (strActionFilter.contains("nhanTin")){
- // if (checkPermission(info, "SEND_SMS")== 1){
- // check = 1;
- // };
- // } else if (strActionFilter.contains("goiDien")){
- // if (checkPermission(info, "SEND_SMS")== 1){
- // check = 1;
- // };
- // } else if (strActionFilter.contains("dungMang")){
- // if (checkPermission(info, "SEND_SMS")== 1){
- // check = 1;
- // };
- // } else if (strActionFilter.contains("layThongTin")){
- // if (checkPermission(info, "SEND_SMS")== 1){
- // check = 1;
- // };
- // }
- // if (check == 0){
- // applist.add(info);
- // }
- //
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // }
- //
- // return applist;
- // }
- private List<ApplicationInfo> filterForLaunchIntent(
- List<ApplicationInfo> list, String strActionFilter) {
- ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
- for (ApplicationInfo info : list) {
- try {
- if (null != packageManager
- .getLaunchIntentForPackage(info.packageName)) {
- // Log.d("test permission", "thieumao " + info.packageName);
- /*
- SEND_SMS nhanTin
- CALL_PHONE goiDien
- INTERNET dungMang
- GET_ACCOUNTS layThongTin
- */
- int check = 0;
- if (strActionFilter.contains("nhanTin")){
- if (checkPermission(info, "SEND_SMS")== 1){
- check = 1;
- };
- } else if (strActionFilter.contains("goiDien")){
- if (checkPermission(info, "CALL_PHONE")== 1){
- check = 1;
- };
- } else if (strActionFilter.contains("dungMang")){
- if (checkPermission(info, "INTERNET")== 1){
- check = 1;
- };
- } else if (strActionFilter.contains("layThongTin")){
- if (checkPermission(info, "GET_ACCOUNTS")== 1){
- check = 1;
- };
- }
- if (check == 1){
- applist.add(info);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return applist;
- }
- private int checkPermission(ApplicationInfo applicationInfo, String strPermission){
- try {
- PackageInfo packageInfo = packageManager.getPackageInfo(
- applicationInfo.packageName,
- PackageManager.GET_PERMISSIONS);
- // appNameAndPermissions.append(packageInfo.packageName + "*:\n");
- // Get Permissions
- String[] requestedPermissions = packageInfo.requestedPermissions;
- if (requestedPermissions != null) {
- for (int i = 0; i < requestedPermissions.length; i++) {
- if (requestedPermissions[i].contains(strPermission)){
- return 1;
- }
- Log.d("test permission", "mao "+requestedPermissions[i]);
- // appNameAndPermissions.append(requestedPermissions[i]
- // + "\n");
- }
- // appNameAndPermissions.append("\n");
- }
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- return 0;
- }
- return 0;
- }
- private class LoadApplications extends AsyncTask<Void, Void, Void> {
- private ProgressDialog progress = null;
- @Override
- protected Void doInBackground(Void... params) {
- // applist = checkForLaunchIntent(packageManager
- // .getInstalledApplications(PackageManager.GET_META_DATA));
- applist = filterForLaunchIntent(packageManager
- .getInstalledApplications(PackageManager.GET_META_DATA), strActionFilter);
- listadaptor = new ApplicationAdapter(ListFilterActivity.this,
- R.layout.snippet_list_row, applist);
- return null;
- }
- @Override
- protected void onCancelled() {
- super.onCancelled();
- }
- @Override
- protected void onPostExecute(Void result) {
- setListAdapter(listadaptor);
- progress.dismiss();
- super.onPostExecute(result);
- }
- @Override
- protected void onPreExecute() {
- progress = ProgressDialog.show(ListFilterActivity.this, null,
- "Loading application info...");
- super.onPreExecute();
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment