Example usage for android.app Activity getPackageManager

List of usage examples for android.app Activity getPackageManager

Introduction

In this page you can find the example usage for android.app Activity getPackageManager.

Prototype

@Override
    public PackageManager getPackageManager() 

Source Link

Usage

From source file:de.grobox.liberario.fragments.AboutFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_about, container, false);
    Activity activity = getActivity();

    String versionName;/*from   w  w  w  .j  ava2  s .c  o m*/
    try {
        versionName = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        versionName = "?.?";
    }

    // add app name and version
    TextView aboutApp = (TextView) view.findViewById(R.id.aboutApp);
    aboutApp.setText(getResources().getString(R.string.app_name) + "  " + versionName);

    // create real paragraphs
    TextView t = (TextView) view.findViewById(R.id.aboutTextView);
    t.setText(Html.fromHtml(getString(R.string.about)
            + String.format(getString(R.string.about_bottom), getString(R.string.website),
                    getString(R.string.bugtracker), getString(R.string.website) + "#donate")));

    // make links in about text clickable
    t.setMovementMethod(LinkMovementMethod.getInstance());
    t.setLinkTextColor(ContextCompat.getColor(activity, R.color.accent));

    Button website = (Button) view.findViewById(R.id.websiteButton);
    website.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.website)));
            startActivity(launchBrowser);
        }
    });

    return view;
}

From source file:com.example.android.apprestrictionenforcer.StatusFragment.java

private void updateUi(Activity activity) {
    PackageManager packageManager = activity.getPackageManager();
    try {/*from  w  ww  .  j a v a  2  s . c om*/
        ApplicationInfo info = packageManager.getApplicationInfo(Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA,
                PackageManager.GET_UNINSTALLED_PACKAGES);
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) activity
                .getSystemService(Activity.DEVICE_POLICY_SERVICE);
        if ((info.flags & ApplicationInfo.FLAG_INSTALLED) != 0) {
            if (!devicePolicyManager.isApplicationHidden(EnforcerDeviceAdminReceiver.getComponentName(activity),
                    Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA)) {
                // The app is ready to enforce restrictions
                // This is unlikely to happen in this sample as unhideApp() handles it.
                mListener.onStatusUpdated();
            } else {
                // The app is installed but hidden in this profile
                mTextStatus.setText(R.string.status_not_activated);
                mButtonUnhide.setVisibility(View.VISIBLE);
            }
        } else {
            // Need to reinstall the sample app
            mTextStatus.setText(R.string.status_need_reinstall);
            mButtonUnhide.setVisibility(View.GONE);
        }
    } catch (PackageManager.NameNotFoundException e) {
        // Need to reinstall the sample app
        mTextStatus.setText(R.string.status_need_reinstall);
        mButtonUnhide.setVisibility(View.GONE);
    }
}

From source file:org.apache.cordova.plugin.SmsInboxPlugin.java

@Override
public boolean execute(String action, JSONArray arg1, final CallbackContext callbackContext)
        throws JSONException {

    if (action.equals(ACTION_HAS_SMS_POSSIBILITY)) {

        Activity ctx = this.cordova.getActivity();
        if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
        } else {/*  ww  w. j a v a 2s  . com*/
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, false));
        }
        return true;
    } else if (action.equals(ACTION_RECEIVE_SMS)) {

        // if already receiving (this case can happen if the startReception is called
        // several times
        if (this.isReceiving) {
            // close the already opened callback ...
            PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
            pluginResult.setKeepCallback(false);
            this.callback_receive.sendPluginResult(pluginResult);

            // ... before registering a new one to the sms receiver
        }
        this.isReceiving = true;

        if (this.smsReceiver == null) {
            this.smsReceiver = new SmsReceiver();
            IntentFilter fp = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
            fp.setPriority(1000);
            // fp.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
            this.cordova.getActivity().registerReceiver(this.smsReceiver, fp);
        }

        this.smsReceiver.startReceiving(callbackContext);

        PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
        pluginResult.setKeepCallback(true);
        callbackContext.sendPluginResult(pluginResult);
        this.callback_receive = callbackContext;

        return true;
    } else if (action.equals(ACTION_STOP_RECEIVE_SMS)) {

        if (this.smsReceiver != null) {
            smsReceiver.stopReceiving();
        }

        this.isReceiving = false;

        // 1. Stop the receiving context
        PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
        pluginResult.setKeepCallback(false);
        this.callback_receive.sendPluginResult(pluginResult);

        // 2. Send result for the current context
        pluginResult = new PluginResult(PluginResult.Status.OK);
        callbackContext.sendPluginResult(pluginResult);

        return true;
    }

    return false;
}

From source file:io.palaima.debugdrawer.modules.BuildModule.java

@Override
public void onAttachActivity(Activity activity) {
    super.onAttachActivity(activity);
    try {//w  w w. ja  va  2  s. c  om
        info = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:nl.vanvianen.mapsme.MapsmeModule.java

@Kroll.method
@SuppressWarnings("unchecked")
public Boolean openMapsMe(HashMap options) {
    Activity activity = TiApplication.getInstance().getCurrentActivity();
    boolean mapsMeInstalled = activity.getPackageManager()
            .resolveActivity(new Intent("com.mapswithme.maps.api.request"), 0) != null;

    if (mapsMeInstalled) {
        double lat = 0.0;
        double lon = 0.0;
        String name = null;/* w  w w. j av  a  2 s .  co m*/
        double zoom = -1;

        if (options.get("lat") instanceof Double) {
            lat = (Double) options.get("lat");
        }
        if (options.get("lon") instanceof Double) {
            lon = (Double) options.get("lon");
        }
        if (options.get("name") instanceof String) {
            name = (String) options.get("name");
        }
        if (options.get("zoom") instanceof Double) {
            zoom = (Double) options.get("zoom");
        }

        Log.d(LCAT, "Open Maps.ME, coordinates=" + lat + "," + lon + ", name=" + name + ", zoom=" + zoom);

        MapsWithMeApi.showPointOnMap(activity, lat, lon, name, zoom);
    } else {
        Log.d(LCAT, "Maps.ME not installed");
    }

    return mapsMeInstalled;
}

From source file:com.brq.wallet.activity.main.BalanceMasterFragment.java

@Override
public void onResume() {
    Activity activity = getActivity();
    // Set beta build
    PackageInfo pInfo;//  ww w .j a  v a  2 s . c om
    try {
        pInfo = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);
        ((TextView) activity.findViewById(R.id.tvBuildText))
                .setText(getResources().getString(R.string.build_text, pInfo.versionName));
    } catch (NameNotFoundException e) {
        // Ignore
        //todo insert uncaught error handler
    }

    MbwManager mbwManager = MbwManager.getInstance(activity);
    tvTor = (TextView) activity.findViewById(R.id.tvTorState);
    if (mbwManager.getTorMode() == ServerEndpointType.Types.ONLY_TOR && mbwManager.getTorManager() != null) {
        tvTor.setVisibility(View.VISIBLE);
        showTorState(mbwManager.getTorManager().getInitState());
    } else {
        tvTor.setVisibility(View.GONE);
    }

    MbwManager.getInstance(this.getActivity()).getEventBus().register(this);
    super.onResume();
}

From source file:org.apache.cordova.SMSComposer.java

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    System.out.println("MADE IT HERE..." + action);
    JSONObject parameters = args.getJSONObject(0);

    if (action.equals(ACTION_HAS_SMS_POSSIBILITY)) {

        Activity ctx = this.cordova.getActivity();
        if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
        } else {//from   w w w  . ja v a  2s .  c  o m
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, false));
        }
        return true;
    } else if (action.equals(ACTION_SEND_SMS)) {
        System.out.println("MADE INTO HERE TOO...");
        try {
            System.out.println(parameters.toString());
            JSONArray phoneNumbers = parameters.getJSONArray("toRecipients");
            System.out.println("json 1 worked");
            System.out.println("1" + phoneNumbers.length());
            System.out.println("2" + phoneNumbers.toString());
            System.out.println("3" + phoneNumbers.get(0));
            System.out.println("4" + phoneNumbers.getString(0));
            String[] numbers = null;

            String message = parameters.getString("body");
            System.out.println("json 2 worked");
            System.out.println(message);

            if (phoneNumbers != null && phoneNumbers.length() > 0 && phoneNumbers.toString().equals("[\"\"]")) {
                numbers = new String[phoneNumbers.length()];
                System.out.println("string array created");
                for (int i = 0; i < phoneNumbers.length(); i++) {
                    numbers[i] = phoneNumbers.getString(i);
                    System.out.println(numbers[i]);
                }

                this.sendSMS(numbers, message);
            } else {
                this.openSMSComposer(message); // if the list is empty, open
                // composer instead.
            }

            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
        } catch (JSONException ex) {
            System.out.println("JSON ERRORS");
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, ex.getMessage()));
        } catch (Exception e) {
            System.out.println("Error handling: " + e.toString());
        }

        return true;
    }
    System.out.println("Error with api");
    callbackContext
            .sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Error, action doesnt fit api"));
    return false;
}

From source file:de.grobox.transportr.about.AboutFragment.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_about, container, false);
    Activity activity = getActivity();

    String versionName;//from   w  ww.  ja v  a2 s.  c o m
    try {
        if (activity == null)
            throw new NameNotFoundException();
        versionName = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e) {
        versionName = "?.?";
    }

    // add app name and version
    TextView aboutApp = view.findViewById(R.id.aboutApp);
    aboutApp.setText(getResources().getString(R.string.app_name) + "  " + versionName);

    // create real paragraphs
    TextView t = view.findViewById(R.id.aboutTextView);
    t.setText(Html.fromHtml(getString(R.string.about)
            + String.format(getString(R.string.about_bottom), getString(R.string.website),
                    getString(R.string.bugtracker), getString(R.string.website) + "#donate")));

    // make links in about text clickable
    t.setMovementMethod(LinkMovementMethod.getInstance());
    t.setLinkTextColor(ContextCompat.getColor(getContext(), R.color.accent));

    Button website = view.findViewById(R.id.websiteButton);
    website.setOnClickListener(v -> {
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.website)));
        startActivity(launchBrowser);
    });

    return view;
}

From source file:com.nick.scalpel.core.request.RequestPermissionWirer.java

private String[] extractUnGranted(Activity activity, String[] declaredPerms) {
    if (declaredPerms == null || declaredPerms.length == 0)
        return null;
    PackageManager packageManager = activity.getPackageManager();
    List<String> requestList = new ArrayList<>(declaredPerms.length);
    for (String info : declaredPerms) {
        int code = packageManager.checkPermission(info, activity.getPackageName());
        if (code == PackageManager.PERMISSION_GRANTED)
            continue;
        logV("Will request perm:" + info + ", current code:" + code);
        requestList.add(info);//from  w w w .j  a  va2 s .c om
    }
    String[] out = new String[requestList.size()];
    for (int i = 0; i < requestList.size(); i++) {
        out[i] = requestList.get(i);
    }
    return out;
}

From source file:dev.nick.accessories.injection.processors.PermissionProcessor.java

private String[] extractUnGranted(Activity activity, String[] declaredPerms) {
    if (declaredPerms == null || declaredPerms.length == 0)
        return null;
    PackageManager packageManager = activity.getPackageManager();
    List<String> requestList = new ArrayList<>(declaredPerms.length);
    for (String info : declaredPerms) {
        int code = packageManager.checkPermission(info, activity.getPackageName());
        if (code == PackageManager.PERMISSION_GRANTED)
            continue;
        report("Will request perm:" + info + ", current code:" + code);
        requestList.add(info);/*w ww .  j  a  va 2s  . co m*/
    }
    String[] out = new String[requestList.size()];
    for (int i = 0; i < requestList.size(); i++) {
        out[i] = requestList.get(i);
    }
    return out;
}