Friday, September 29, 2017

android - Intent for app details settings

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#d4eaad"
    android:padding="16dp"
    >
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.HashMap;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widgets reference from xml layout
        mListView = findViewById(R.id.list_view);

        // Get the installed package list
        HashMap<String,String> map = getInstalledPackages();

        // Get the values array from hash map
        final String[] values = map.values().toArray(new String[map.size()]);
        final String[] keys = map.keySet().toArray(new String[map.size()]);

        // Initialize a new array adapter instance
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                mActivity,
                android.R.layout.simple_list_item_1,
                values
        );

        // Set the list view adapter
        mListView.setAdapter(adapter);

        // Set a item click listener for list view
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String packageName = keys[i];
                String label = values[i];

                // Go to application details settings
                displayAppDetailsSettings(packageName);
            }
        });
    }


    // Custom method to get all installed package names
    protected HashMap<String,String> getInstalledPackages(){
        PackageManager packageManager = getPackageManager();

        // Initialize a new intent
        Intent intent = new Intent(Intent.ACTION_MAIN,null);
        // Set the intent category
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        // Set the intent flags
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                |Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

        // Initialize a new list of resolve info
        List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(intent,0);

        // Initialize a new hash map of package names and application label
        HashMap<String,String> map = new HashMap<>();

        // Loop through the resolve info list
        for(ResolveInfo resolveInfo : resolveInfoList){
            // Get the activity info from resolve info
            ActivityInfo activityInfo = resolveInfo.activityInfo;

            // Get the package name
            String packageName = activityInfo.applicationInfo.packageName;

            // Get the application label
            String label = (String) packageManager.getApplicationLabel(activityInfo.applicationInfo);

            // Put the package name and application label to hash map
            map.put(packageName,label);
        }
        return map;
    }


    // Custom method go to application details settings by package name
    protected void displayAppDetailsSettings(String packageName){
        try{
            /*
                Settings
                    The Settings provider contains global system-level device preferences.
            */
            /*
                String ACTION_APPLICATION_DETAILS_SETTINGS

                    Activity Action : Show screen of details about a particular application.

                    In some cases, a matching Activity may not exist,
                    so ensure you safeguard against this.

                    Input : The Intent's data URI specifies the application package name to be shown,
                    with the "package" scheme. That is "package:com.my.app".

                    Output : Nothing.

                    Constant Value : "android.settings.APPLICATION_DETAILS_SETTINGS"
            */
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + packageName));
            startActivity(intent);
        }catch (ActivityNotFoundException e){
            e.printStackTrace();
            /*
                String ACTION_MANAGE_APPLICATIONS_SETTINGS
                    Activity Action : Show settings to manage installed applications.

                    In some cases, a matching Activity may not exist, so ensure you
                    safeguard against this.

                    Input : Nothing.

                    Output : Nothing.

                    Constant Value : "android.settings.MANAGE_APPLICATIONS_SETTINGS"
            */
            Intent alternateIntent = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
            startActivity(alternateIntent);
        }
    }
}