Example usage for android.content.pm PackageInfo PackageInfo

List of usage examples for android.content.pm PackageInfo PackageInfo

Introduction

In this page you can find the example usage for android.content.pm PackageInfo PackageInfo.

Prototype

public PackageInfo() 

Source Link

Usage

From source file:Main.java

public static int getVersionCode(Context context) {
    PackageManager manager = context.getPackageManager();
    PackageInfo pInfo = new PackageInfo();
    try {//from  w ww  .ja va  2  s  .com
        pInfo = manager.getPackageInfo(context.getPackageName(), 0);
        return pInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static String getVersionName(Context context) {
    PackageManager manager = context.getPackageManager();
    PackageInfo pInfo = new PackageInfo();
    try {//from   ww w .  j  a  va  2  s . c  om
        pInfo = manager.getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return pInfo.versionName == null ? null : pInfo.versionName;
}

From source file:Main.java

private static PackageInfo getPackageInfo(Context context, String pkgName) {
    PackageInfo packageInfo = null;//from   w w  w. j a v  a  2s.  c o m
    try {
        PackageManager pm = context.getPackageManager();
        packageInfo = pm.getPackageInfo(pkgName, 0);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        packageInfo = new PackageInfo();
        packageInfo.versionCode = 1;
        packageInfo.versionName = "1.0";
    }
    return packageInfo;
}

From source file:ly.count.android.api.DeviceInfoTests.java

public void testGetAppVersion() throws PackageManager.NameNotFoundException {
    final PackageInfo pkgInfo = new PackageInfo();
    pkgInfo.versionName = "42.0";
    final String fakePkgName = "i.like.chicken";
    final PackageManager mockPkgMgr = mock(PackageManager.class);
    when(mockPkgMgr.getPackageInfo(fakePkgName, 0)).thenReturn(pkgInfo);
    final Context mockContext = mock(Context.class);
    when(mockContext.getPackageName()).thenReturn(fakePkgName);
    when(mockContext.getPackageManager()).thenReturn(mockPkgMgr);
    assertEquals("42.0", DeviceInfo.getAppVersion(mockContext));
}

From source file:eu.masconsult.bgbanking.activity.HomeActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuItem refreshItem = menu.add(R.string.menu_item_refresh);
    refreshItem.setIcon(R.drawable.ic_menu_refresh);
    refreshItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    refreshItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override//from   w w w .  j  a  va 2s .c o  m
        public boolean onMenuItemClick(MenuItem item) {
            AccountManager accountManager = AccountManager.get(HomeActivity.this);
            if (accountManager == null) {
                return false;
            }

            long cnt = 0;
            Bank[] banks = Bank.values();
            for (Bank bank : banks) {
                for (Account account : accountManager
                        .getAccountsByType(bank.getAccountType(HomeActivity.this))) {
                    Log.v(TAG, String.format("account: %s, %s, %s", account.name, account.type,
                            ContentResolver.getIsSyncable(account, BankingContract.AUTHORITY)));
                    Bundle bundle = new Bundle();
                    bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
                    ContentResolver.requestSync(account, BankingContract.AUTHORITY, bundle);
                    cnt++;
                }
            }

            EasyTracker.getTracker().trackEvent("ui_action", "menu_item_click", "refresh", cnt);
            return true;
        }
    });

    MenuItem addAccountItem = menu.add(R.string.menu_item_add_account);
    addAccountItem.setIcon(R.drawable.ic_menu_add);
    addAccountItem
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    addAccountItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            addAccount();
            EasyTracker.getTracker().trackEvent("ui_action", "menu_item_click", "add_account", 1l);
            return true;
        }
    });

    MenuItem sendFeedback = menu.add(R.string.menu_item_send_feedback);
    sendFeedback.setIcon(R.drawable.ic_menu_start_conversation);
    sendFeedback.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    sendFeedback.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            PackageInfo manager;
            try {
                manager = getPackageManager().getPackageInfo(getPackageName(), 0);
            } catch (NameNotFoundException e) {
                manager = new PackageInfo();
                manager.versionCode = 0;
                manager.versionName = "undef";
            }
            try {
                startActivity(new Intent(Intent.ACTION_SEND).setType("plain/text")
                        .putExtra(Intent.EXTRA_EMAIL, new String[] { "bgbanks@masconsult.eu" })
                        .putExtra(Intent.EXTRA_SUBJECT,
                                "BG Banks v" + manager.versionName + "-" + manager.versionCode + " feedback"));
                EasyTracker.getTracker().trackEvent("ui_action", "menu_item_click", "send_feedback", 1l);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    });

    return true;
}

From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java

public static String currentDirectory() {
    PackageManager pkgManager = MyApp.context.getPackageManager();
    String pkgName = MyApp.context.getPackageName();
    PackageInfo pkg = new PackageInfo();

    try {//  ww w  .  j  a  v  a2 s .  c om
        pkg = pkgManager.getPackageInfo(pkgName, 0);
    } catch (Throwable e) {
        Log.i(MyApp.TAG, e.getMessage().toString());
    }

    return pkg.applicationInfo.dataDir;
}

From source file:android.content.pm.PackageParser.java

public static PackageInfo generatePackageInfo(PackageParser.Package p, int gids[], int flags,
        long firstInstallTime, long lastUpdateTime, Set<String> grantedPermissions, PackageUserState state,
        int userId) {

    if (!checkUseInstalledOrHidden(flags, state)) {
        return null;
    }//from   ww w.j ava2 s . com
    PackageInfo pi = new PackageInfo();
    pi.packageName = p.packageName;
    pi.splitNames = p.splitNames;
    pi.versionCode = p.mVersionCode;
    pi.baseRevisionCode = p.baseRevisionCode;
    pi.splitRevisionCodes = p.splitRevisionCodes;
    pi.versionName = p.mVersionName;
    pi.sharedUserId = p.mSharedUserId;
    pi.sharedUserLabel = p.mSharedUserLabel;
    pi.applicationInfo = generateApplicationInfo(p, flags, state, userId);
    pi.installLocation = p.installLocation;
    pi.coreApp = p.coreApp;
    if ((pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0
            || (pi.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
        pi.requiredForAllUsers = p.mRequiredForAllUsers;
    }
    pi.restrictedAccountType = p.mRestrictedAccountType;
    pi.requiredAccountType = p.mRequiredAccountType;
    pi.overlayTarget = p.mOverlayTarget;
    pi.firstInstallTime = firstInstallTime;
    pi.lastUpdateTime = lastUpdateTime;
    if ((flags & PackageManager.GET_GIDS) != 0) {
        pi.gids = gids;
    }
    if ((flags & PackageManager.GET_CONFIGURATIONS) != 0) {
        int N = p.configPreferences != null ? p.configPreferences.size() : 0;
        if (N > 0) {
            pi.configPreferences = new ConfigurationInfo[N];
            p.configPreferences.toArray(pi.configPreferences);
        }
        N = p.reqFeatures != null ? p.reqFeatures.size() : 0;
        if (N > 0) {
            pi.reqFeatures = new FeatureInfo[N];
            p.reqFeatures.toArray(pi.reqFeatures);
        }
        N = p.featureGroups != null ? p.featureGroups.size() : 0;
        if (N > 0) {
            pi.featureGroups = new FeatureGroupInfo[N];
            p.featureGroups.toArray(pi.featureGroups);
        }
    }
    if ((flags & PackageManager.GET_ACTIVITIES) != 0) {
        int N = p.activities.size();
        if (N > 0) {
            if ((flags & PackageManager.GET_DISABLED_COMPONENTS) != 0) {
                pi.activities = new ActivityInfo[N];
            } else {
                int num = 0;
                for (int i = 0; i < N; i++) {
                    if (p.activities.get(i).info.enabled)
                        num++;
                }
                pi.activities = new ActivityInfo[num];
            }
            for (int i = 0, j = 0; i < N; i++) {
                final Activity activity = p.activities.get(i);
                if (activity.info.enabled || (flags & PackageManager.GET_DISABLED_COMPONENTS) != 0) {
                    pi.activities[j++] = generateActivityInfo(p.activities.get(i), flags, state, userId);
                }
            }
        }
    }
    if ((flags & PackageManager.GET_RECEIVERS) != 0) {
        int N = p.receivers.size();
        if (N > 0) {
            if ((flags & PackageManager.GET_DISABLED_COMPONENTS) != 0) {
                pi.receivers = new ActivityInfo[N];
            } else {
                int num = 0;
                for (int i = 0; i < N; i++) {
                    if (p.receivers.get(i).info.enabled)
                        num++;
                }
                pi.receivers = new ActivityInfo[num];
            }
            for (int i = 0, j = 0; i < N; i++) {
                final Activity activity = p.receivers.get(i);
                if (activity.info.enabled || (flags & PackageManager.GET_DISABLED_COMPONENTS) != 0) {
                    pi.receivers[j++] = generateActivityInfo(p.receivers.get(i), flags, state, userId);
                }
            }
        }
    }
    if ((flags & PackageManager.GET_SERVICES) != 0) {
        int N = p.services.size();
        if (N > 0) {
            if ((flags & PackageManager.GET_DISABLED_COMPONENTS) != 0) {
                pi.services = new ServiceInfo[N];
            } else {
                int num = 0;
                for (int i = 0; i < N; i++) {
                    if (p.services.get(i).info.enabled)
                        num++;
                }
                pi.services = new ServiceInfo[num];
            }
            for (int i = 0, j = 0; i < N; i++) {
                final Service service = p.services.get(i);
                if (service.info.enabled || (flags & PackageManager.GET_DISABLED_COMPONENTS) != 0) {
                    pi.services[j++] = generateServiceInfo(p.services.get(i), flags, state, userId);
                }
            }
        }
    }
    if ((flags & PackageManager.GET_PROVIDERS) != 0) {
        int N = p.providers.size();
        if (N > 0) {
            if ((flags & PackageManager.GET_DISABLED_COMPONENTS) != 0) {
                pi.providers = new ProviderInfo[N];
            } else {
                int num = 0;
                for (int i = 0; i < N; i++) {
                    if (p.providers.get(i).info.enabled)
                        num++;
                }
                pi.providers = new ProviderInfo[num];
            }
            for (int i = 0, j = 0; i < N; i++) {
                final Provider provider = p.providers.get(i);
                if (provider.info.enabled || (flags & PackageManager.GET_DISABLED_COMPONENTS) != 0) {
                    pi.providers[j++] = generateProviderInfo(p.providers.get(i), flags, state, userId);
                }
            }
        }
    }
    if ((flags & PackageManager.GET_INSTRUMENTATION) != 0) {
        int N = p.instrumentation.size();
        if (N > 0) {
            pi.instrumentation = new InstrumentationInfo[N];
            for (int i = 0; i < N; i++) {
                pi.instrumentation[i] = generateInstrumentationInfo(p.instrumentation.get(i), flags);
            }
        }
    }
    if ((flags & PackageManager.GET_PERMISSIONS) != 0) {
        int N = p.permissions.size();
        if (N > 0) {
            pi.permissions = new PermissionInfo[N];
            for (int i = 0; i < N; i++) {
                pi.permissions[i] = generatePermissionInfo(p.permissions.get(i), flags);
            }
        }
        N = p.requestedPermissions.size();
        if (N > 0) {
            pi.requestedPermissions = new String[N];
            pi.requestedPermissionsFlags = new int[N];
            for (int i = 0; i < N; i++) {
                final String perm = p.requestedPermissions.get(i);
                pi.requestedPermissions[i] = perm;
                // The notion of required permissions is deprecated but for compatibility.
                pi.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_REQUIRED;
                if (grantedPermissions != null && grantedPermissions.contains(perm)) {
                    pi.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_GRANTED;
                }
            }
        }
    }
    if ((flags & PackageManager.GET_SIGNATURES) != 0) {
        int N = (p.mSignatures != null) ? p.mSignatures.length : 0;
        if (N > 0) {
            pi.signatures = new Signature[N];
            System.arraycopy(p.mSignatures, 0, pi.signatures, 0, N);
        }
    }
    return pi;
}