Example usage for android.content Context USER_SERVICE

List of usage examples for android.content Context USER_SERVICE

Introduction

In this page you can find the example usage for android.content Context USER_SERVICE.

Prototype

String USER_SERVICE

To view the source code for android.content Context USER_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.os.UserManager for managing users on devices that support multiple users.

Usage

From source file:Main.java

@SuppressLint("InlinedApi")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static boolean hasSyncPermissions(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2)
        return true;

    UserManager manager = (UserManager) context.getSystemService(Context.USER_SERVICE);
    Bundle userRestrictions = manager.getUserRestrictions();
    return !userRestrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false);
}

From source file:Main.java

public static boolean isUnableToModifyAccounts(Context context) {
    UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
    Bundle restrictions = um.getUserRestrictions();
    return restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false);
}

From source file:Main.java

@SuppressLint("NewApi")
public static boolean isAdminUser(Context context) {
    final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
    try {/* w w w  . j av  a2  s. c  om*/
        Method getUserHandle = UserManager.class.getMethod("getUserHandle");
        int userHandle = (Integer) getUserHandle.invoke(um);
        return userHandle == 0;
    } catch (Exception ex) {
        return true;
    }
}

From source file:Main.java

@SuppressLint("NewApi")
public static boolean supportsMultipleUsers(Context context) {
    final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
    try {/*from   w ww  .  j ava  2 s .c o m*/
        Method supportsMultipleUsers = UserManager.class.getMethod("supportsMultipleUsers");
        return (Boolean) supportsMultipleUsers.invoke(um);
    } catch (Exception ex) {
        return false;
    }
}

From source file:com.afwsamples.testdpc.AddAccountActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
    mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    mAdminComponentName = DeviceAdminReceiver.getComponentName(this);

    setContentView(R.layout.activity_add_account);
    SetupWizardLayout layout = (SetupWizardLayout) findViewById(R.id.setup_wizard_layout);
    layout.getNavigationBar().setNavigationBarListener(this);
    NavigationBar navigationBar = layout.getNavigationBar();
    navigationBar.getBackButton().setEnabled(false);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        mNextActivityIntent = (Intent) extras.get(EXTRA_NEXT_ACTIVITY_INTENT);
    }//ww w  . jav a 2  s  . c o  m
}

From source file:com.farmerbb.taskbar.receiver.ReceiveSettingsReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    // Ignore this broadcast if this is the free version
    if (BuildConfig.APPLICATION_ID.equals(BuildConfig.PAID_APPLICATION_ID)) {
        // Get pinned and blocked apps
        PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
        pba.clear(context);/*from   ww  w .  j a v  a 2  s  .c  o m*/

        String[] pinnedAppsPackageNames = intent.getStringArrayExtra("pinned_apps_package_names");
        String[] pinnedAppsComponentNames = intent.getStringArrayExtra("pinned_apps_component_names");
        String[] pinnedAppsLabels = intent.getStringArrayExtra("pinned_apps_labels");
        long[] pinnedAppsUserIds = intent.getLongArrayExtra("pinned_apps_user_ids");

        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);

        if (pinnedAppsPackageNames != null && pinnedAppsComponentNames != null && pinnedAppsLabels != null)
            for (int i = 0; i < pinnedAppsPackageNames.length; i++) {
                Intent throwaway = new Intent();
                throwaway.setComponent(ComponentName.unflattenFromString(pinnedAppsComponentNames[i]));

                long userId;
                if (pinnedAppsUserIds != null)
                    userId = pinnedAppsUserIds[i];
                else
                    userId = userManager.getSerialNumberForUser(Process.myUserHandle());

                AppEntry newEntry = new AppEntry(pinnedAppsPackageNames[i], pinnedAppsComponentNames[i],
                        pinnedAppsLabels[i],
                        IconCache.getInstance(context).getIcon(context, context.getPackageManager(),
                                launcherApps.resolveActivity(throwaway,
                                        userManager.getUserForSerialNumber(userId))),
                        true);

                newEntry.setUserId(userId);
                pba.addPinnedApp(context, newEntry);
            }

        String[] blockedAppsPackageNames = intent.getStringArrayExtra("blocked_apps_package_names");
        String[] blockedAppsComponentNames = intent.getStringArrayExtra("blocked_apps_component_names");
        String[] blockedAppsLabels = intent.getStringArrayExtra("blocked_apps_labels");

        if (blockedAppsPackageNames != null && blockedAppsComponentNames != null && blockedAppsLabels != null)
            for (int i = 0; i < blockedAppsPackageNames.length; i++) {
                pba.addBlockedApp(context, new AppEntry(blockedAppsPackageNames[i],
                        blockedAppsComponentNames[i], blockedAppsLabels[i], null, false));
            }

        // Get blacklist
        Blacklist blacklist = Blacklist.getInstance(context);
        blacklist.clear(context);

        String[] blacklistPackageNames = intent.getStringArrayExtra("blacklist_package_names");
        String[] blacklistLabels = intent.getStringArrayExtra("blacklist_labels");

        if (blacklistPackageNames != null && blacklistLabels != null)
            for (int i = 0; i < blacklistPackageNames.length; i++) {
                blacklist.addBlockedApp(context,
                        new BlacklistEntry(blacklistPackageNames[i], blacklistLabels[i]));
            }

        // Get top apps
        TopApps topApps = TopApps.getInstance(context);
        topApps.clear(context);

        String[] topAppsPackageNames = intent.getStringArrayExtra("top_apps_package_names");
        String[] topAppsLabels = intent.getStringArrayExtra("top_apps_labels");

        if (topAppsPackageNames != null && topAppsLabels != null)
            for (int i = 0; i < topAppsPackageNames.length; i++) {
                topApps.addTopApp(context, new BlacklistEntry(topAppsPackageNames[i], topAppsLabels[i]));
            }

        // Get saved window sizes
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            SavedWindowSizes savedWindowSizes = SavedWindowSizes.getInstance(context);
            savedWindowSizes.clear(context);

            String[] savedWindowSizesComponentNames = intent
                    .getStringArrayExtra("saved_window_sizes_component_names");
            String[] savedWindowSizesWindowSizes = intent
                    .getStringArrayExtra("saved_window_sizes_window_sizes");

            if (savedWindowSizesComponentNames != null && savedWindowSizesWindowSizes != null)
                for (int i = 0; i < savedWindowSizesComponentNames.length; i++) {
                    savedWindowSizes.setWindowSize(context, savedWindowSizesComponentNames[i],
                            savedWindowSizesWindowSizes[i]);
                }
        }

        // Get shared preferences
        String contents = intent.getStringExtra("preferences");
        if (contents.length() > 0)
            try {
                File file = new File(context.getFilesDir().getParent() + "/shared_prefs/"
                        + BuildConfig.APPLICATION_ID + "_preferences.xml");
                FileOutputStream output = new FileOutputStream(file);
                output.write(contents.getBytes());
                output.close();
            } catch (IOException e) {
                /* Gracefully fail */ }

        try {
            File file = new File(context.getFilesDir() + File.separator + "imported_successfully");
            if (file.createNewFile())
                LocalBroadcastManager.getInstance(context)
                        .sendBroadcast(new Intent("com.farmerbb.taskbar.IMPORT_FINISHED"));
        } catch (IOException e) {
            /* Gracefully fail */ }
    }
}

From source file:com.android.cts.verifier.managedprovisioning.NfcTestActivity.java

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

    mAdminReceiverComponent = new ComponentName(this, DeviceAdminTestReceiver.class.getName());
    mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    mUserMangaer = (UserManager) getSystemService(Context.USER_SERVICE);
    mDisallowByPolicy = getIntent().getBooleanExtra(EXTRA_DISALLOW_BY_POLICY, false);
    if (mDisallowByPolicy) {
        mDevicePolicyManager.addUserRestriction(mAdminReceiverComponent, UserManager.DISALLOW_OUTGOING_BEAM);
    }/*from ww  w  . j  ava  2 s . c  o m*/

    final Uri uri = createUriForImage(SAMPLE_IMAGE_FILENAME, SAMPLE_IMAGE_CONTENT);
    Uri[] uris = new Uri[] { uri };

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    mNfcAdapter.setBeamPushUris(uris, this);

    findViewById(R.id.manual_beam_button).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            mNfcAdapter.invokeBeam(NfcTestActivity.this);
        }
    });
    findViewById(R.id.intent_share_button).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.setType("image/jpg");
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            // Specify the package name of NfcBeamActivity so that the tester don't need to
            // select the activity manually.
            shareIntent.setClassName(NFC_BEAM_PACKAGE, NFC_BEAM_ACTIVITY);
            try {
                startActivity(shareIntent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(NfcTestActivity.this, R.string.provisioning_byod_cannot_resolve_beam_activity,
                        Toast.LENGTH_SHORT).show();
                Log.e(TAG, "Nfc beam activity not found", e);
            }
        }
    });
}

From source file:com.android.messaging.util.OsUtil.java

public static boolean isSecondaryUser() {
    if (sIsSecondaryUser == null) {
        final Context context = Factory.get().getApplicationContext();
        boolean isSecondaryUser = false;

        // Only check for newer devices (but not the nexus 10)
        if (OsUtil.sIsAtLeastJB_MR1 && !"Nexus 10".equals(Build.MODEL)) {
            final UserHandle uh = android.os.Process.myUserHandle();
            final UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
            if (userManager != null) {
                final long userSerialNumber = userManager.getSerialNumberForUser(uh);
                isSecondaryUser = (0 != userSerialNumber);
            }/*from  w ww. j  a v a  2 s. c o  m*/
        }
        sIsSecondaryUser = isSecondaryUser;
    }
    return sIsSecondaryUser;
}

From source file:com.afwsamples.testdpc.common.Util.java

/**
 * Return {@code true} iff we are the profile owner of a managed profile.
 * Note that profile owner can be in primary user and secondary user too.
 *///from   w w  w. j  a v a  2  s  .co  m
@TargetApi(VERSION_CODES.N)
public static boolean isManagedProfileOwner(Context context) {
    final DevicePolicyManager dpm = getDevicePolicyManager(context);

    if (BuildCompat.isAtLeastN()) {
        try {
            return dpm.isManagedProfile(DeviceAdminReceiver.getComponentName(context));
        } catch (SecurityException ex) {
            // This is thrown if we are neither profile owner nor device owner.
            return false;
        }
    }
    UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
    return isProfileOwner(context) && userManager.getUserProfiles().size() > 1;
}

From source file:com.afwsamples.testdpc.common.Util.java

@TargetApi(VERSION_CODES.M)
public static boolean isPrimaryUser(Context context) {
    if (isAtLeastM()) {
        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        return userManager.isSystemUser();
    } else {/*from ww w .j  a  v  a2  s  .c  o  m*/
        // Assume only DO can be primary user. This is not perfect but the cases in which it is
        // wrong are uncommon and require adb to set up.
        return isDeviceOwner(context);
    }
}