Skip to content

Instantly share code, notes, and snippets.

@webserveis
Last active October 5, 2023 14:36
Show Gist options
  • Select an option

  • Save webserveis/15fba4e7708a2a21180a6156eb6f7cc1 to your computer and use it in GitHub Desktop.

Select an option

Save webserveis/15fba4e7708a2a21180a6156eb6f7cc1 to your computer and use it in GitHub Desktop.
Get all packages user, system.. in Android

https://stackoverflow.com/questions/17504169/how-to-get-installed-applications-in-android-and-no-system-apps

Get all packages installed in Android //https://github.com/aNNiMON/Lightweight-Stream-API

Common

final PackageManager pm = getPackageManager();

Get all packages

List<ApplicationInfo> packagesAll = pm.getInstalledApplications(PackageManager.GET_META_DATA);

Packages by User

Get all packages installed from user

List<ApplicationInfo> packagesByUser = Stream.of(packagesAll)
       .filter((p -> (p.flags & ApplicationInfo.FLAG_SYSTEM) == 0))
       .map(ApplicationInfo::new)
       .collect(Collectors.toList());

Packages by System

Get all packages installed by system, bloatware...

List<ApplicationInfo> packagesBySystem = Stream.of(packagesAll)
       .filter((p -> (p.flags & ApplicationInfo.FLAG_SYSTEM) != 0))
       .map(ApplicationInfo::new)
       .collect(Collectors.toList());

Packages disabled

Get all packages stay disabled

List<ApplicationInfo> packagesDisabled = Stream.of(packagesAll)
       .filter((p -> (!p.enabled)))
       .map(ApplicationInfo::new)
       .collect(Collectors.toList());

Packages Moved to SD

Get all packages installed in external storages SD-Card

List<ApplicationInfo> packagesExternalSD = Stream.of(packagesAll)
       .filter((p -> (p.flags & ApplicationInfo.FLAG_SYSTEM) == 0)) //packages by User
       .filter((p -> (p.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0))
       .map(ApplicationInfo::new)
       .collect(Collectors.toList());

Other functions

General functions for packages

private boolean isSystemPackage(ApplicationInfo applicationInfo) {
   return ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}

Extract package label

private String getPackageNameLabel(String packageName) {
   try {
       return (String) getPackageManager().getApplicationLabel(getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA));
   } catch (PackageManager.NameNotFoundException e) {
       e.printStackTrace();
       return null;
   }

}

Try if packages move to sd

private boolean tryMoveSDPackage(ApplicationInfo applicationInfo) {
    try {
        PackageInfo packageInfo = getPackageManager().getPackageInfo(applicationInfo.packageName, 0);
        if (packageInfo.installLocation == PackageInfo.INSTALL_LOCATION_AUTO) {
            //Log.i(TAG, "INSTALL_LOCATION_AUTO");
            return true;
        } else if (packageInfo.installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) {
            //Log.d(TAG, "INSTALL_LOCATION_PREFER_EXTERNAL");
            return true;
        } else {
            //Log.w(TAG, "INSTALL_LOCATION_INTERNAL_ONLY");
            return false;
        }

    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}
package com.webserveis.testautoincrement
import android.app.ActivityManager
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
/*
https://inducesmile.com/android/android-list-installed-apps-in-device-programmatically/
https://stackoverflow.com/questions/34642254/what-java-8-stream-collect-equivalents-are-available-in-the-standard-kotlin-libr
https://stackoverflow.com/questions/40632521/how-to-get-hidden-packages-with-devicepolicymanager/50103017#50103017
*/
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.v(TAG, "Allow install apps from external?" + isInstallingUnknownAppsAllowed())
val activityManager =
getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val tasks = activityManager.appTasks
for (task in tasks) {
Log.d(TAG, "stackId: " + task.taskInfo)
}
val systemPackages = getDisabledPackages()
systemPackages.forEach {
Log.d(TAG, "" + it.packageName + it.enabled)
}
//getPackageManager().getInstalledPackages( PackageManager.GET_DISABLED_COMPONENTS or PackageManager.MATCH_UNINSTALLED_PACKAGES)
}
private fun getSystemPackages(): List<ApplicationInfo> {
val packagesAll: List<ApplicationInfo> =
packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
return packagesAll.filter { it.flags and ApplicationInfo.FLAG_SYSTEM != 0 }
}
private fun getUserPackages(): List<ApplicationInfo> {
val packagesAll: List<ApplicationInfo> =
packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
return packagesAll.filter { it.flags and ApplicationInfo.FLAG_SYSTEM == 0 }
}
private fun getDisabledPackages(): List<ApplicationInfo> {
val packagesAll: List<ApplicationInfo> =
packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
return packagesAll
.filter { !it.enabled }
//.filter { !it.enabled }
}
private fun isSystemPackage(pkgInfo: PackageInfo): Boolean {
return pkgInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0
}
private fun isInstallingUnknownAppsAllowed(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
packageManager.canRequestPackageInstalls()
} else
Settings.Secure.getInt(
contentResolver,
Settings.Secure.INSTALL_NON_MARKET_APPS, 0
) > 0
}
companion object {
val TAG: String = MainActivity::class.java.simpleName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment