Example usage for android.os PersistableBundle getInt

List of usage examples for android.os PersistableBundle getInt

Introduction

In this page you can find the example usage for android.os PersistableBundle getInt.

Prototype

public int getInt(String key) 

Source Link

Document

Returns the value associated with the given key, or 0 if no mapping of the desired type exists for the given key.

Usage

From source file:eu.faircode.netguard.ServiceJob.java

private static void submit(Rule rule, PersistableBundle bundle, Context context) {
    PackageManager pm = context.getPackageManager();
    JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    // Get english application label
    String label = null;/*from   w ww.j  av  a 2s  .  c  o m*/
    try {
        Configuration config = new Configuration();
        config.setLocale(new Locale("en"));
        Resources res = pm.getResourcesForApplication(rule.info.packageName);
        res.updateConfiguration(config, res.getDisplayMetrics());
        label = res.getString(rule.info.applicationInfo.labelRes);
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        CharSequence cs = rule.info.applicationInfo.loadLabel(pm);
        if (cs != null)
            label = cs.toString();
    }

    // Add application data
    bundle.putInt("uid", rule.info.applicationInfo.uid);
    bundle.putString("package", rule.info.packageName);
    bundle.putInt("version_code", rule.info.versionCode);
    bundle.putString("version_name", rule.info.versionName);
    bundle.putString("label", label);
    bundle.putInt("system", rule.system ? 1 : 0);
    try {
        bundle.putString("installer", pm.getInstallerPackageName(rule.info.packageName));
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        bundle.putString("installer", null);
    }

    // Cancel overlapping jobs
    for (JobInfo pending : scheduler.getAllPendingJobs()) {
        String type = pending.getExtras().getString("type");
        if (type != null && type.equals(bundle.getString("type"))) {
            if (type.equals("rule")) {
                int uid = pending.getExtras().getInt("uid");
                if (uid == bundle.getInt("uid")) {
                    Log.i(TAG, "Canceling id=" + pending.getId());
                    scheduler.cancel(pending.getId());
                }
            } else if (type.equals("host")) {
                int uid = pending.getExtras().getInt("uid");
                int version = pending.getExtras().getInt("version");
                int protocol = pending.getExtras().getInt("protocol");
                String daddr = pending.getExtras().getString("daddr");
                int dport = pending.getExtras().getInt("dport");
                if (uid == bundle.getInt("uid") && version == bundle.getInt("version")
                        && protocol == bundle.getInt("protocol") && daddr != null
                        && daddr.equals(bundle.getString("daddr")) && dport == bundle.getInt("dport")) {
                    Log.i(TAG, "Canceling id=" + pending.getId());
                    scheduler.cancel(pending.getId());
                }
            }
        }
    }

    // Schedule job
    ComponentName serviceName = new ComponentName(context, ServiceJob.class);
    JobInfo job = new JobInfo.Builder(++id, serviceName).setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setMinimumLatency(Util.isDebuggable(context) ? 10 * 1000 : 60 * 1000).setExtras(bundle)
            .setPersisted(true).build();
    if (scheduler.schedule(job) == JobScheduler.RESULT_SUCCESS)
        Log.i(TAG, "Scheduled job=" + job.getId() + " success");
    else
        Log.e(TAG, "Scheduled job=" + job.getId() + " failed");
}