Search in sources :

Example 36 with AccountManager

use of android.accounts.AccountManager in project android by nextcloud.

the class ManageAccountsActivity method startAccountCreation.

@Override
public void startAccountCreation() {
    AccountManager am = AccountManager.get(getApplicationContext());
    am.addAccount(MainApp.getAccountType(this), null, null, null, this, future -> {
        if (future != null) {
            try {
                Bundle result = future.getResult();
                String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
                accountManager.setCurrentOwnCloudAccount(name);
                userListAdapter = new UserListAdapter(this, accountManager, getUserListItems(), this, multipleAccountsSupported, false);
                recyclerView.setAdapter(userListAdapter);
                runOnUiThread(() -> userListAdapter.notifyDataSetChanged());
            } catch (OperationCanceledException e) {
                Log_OC.d(TAG, "Account creation canceled");
            } catch (Exception e) {
                Log_OC.e(TAG, "Account creation finished in exception: ", e);
            }
        }
    }, handler);
}
Also used : Bundle(android.os.Bundle) OperationCanceledException(android.accounts.OperationCanceledException) UserAccountManager(com.nextcloud.client.account.UserAccountManager) AccountManager(android.accounts.AccountManager) UserListAdapter(com.owncloud.android.ui.adapter.UserListAdapter) OperationCanceledException(android.accounts.OperationCanceledException)

Example 37 with AccountManager

use of android.accounts.AccountManager in project android by nextcloud.

the class FileContentProvider method updateAccountName.

/**
 * Version 10 of database does not modify its scheme. It coincides with the upgrade of the
 * ownCloud account names structure to include in it the path to the server instance. Updating
 * the account names and path to local files in the files table is a must to keep the existing
 * account working and the database clean.
 *
 * @param db Database where table of files is included.
 */
private void updateAccountName(SQLiteDatabase db) {
    Log_OC.d(SQL, "THREAD:  " + Thread.currentThread().getName());
    AccountManager ama = AccountManager.get(getContext());
    try {
        // get accounts from AccountManager ;  we can't be sure if accounts in it are updated or not although
        // we know the update was previously done in {link @FileActivity#onCreate} because the changes through
        // AccountManager are not synchronous
        Account[] accounts = AccountManager.get(getContext()).getAccountsByType(MainApp.getAccountType(mContext));
        String serverUrl;
        String username;
        String oldAccountName;
        String newAccountName;
        String[] accountOwner = new String[1];
        for (Account account : accounts) {
            // build both old and new account name
            serverUrl = ama.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL);
            username = AccountUtils.getUsernameForAccount(account);
            oldAccountName = AccountUtils.buildAccountNameOld(Uri.parse(serverUrl), username);
            newAccountName = AccountUtils.buildAccountName(Uri.parse(serverUrl), username);
            // update values in database
            db.beginTransaction();
            try {
                ContentValues cv = new ContentValues();
                cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, newAccountName);
                accountOwner[0] = oldAccountName;
                int num = db.update(ProviderTableMeta.FILE_TABLE_NAME, cv, ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", accountOwner);
                Log_OC.d(SQL, "Updated account in database: old name == " + oldAccountName + ", new name == " + newAccountName + " (" + num + " rows updated )");
                // update path for downloaded files
                updateDownloadedFiles(db, newAccountName, oldAccountName);
                db.setTransactionSuccessful();
            } catch (SQLException e) {
                Log_OC.e(TAG, "SQL Exception upgrading account names or paths in database", e);
            } finally {
                db.endTransaction();
            }
        }
    } catch (Exception e) {
        Log_OC.e(TAG, "Exception upgrading account names or paths in database", e);
    }
}
Also used : ContentValues(android.content.ContentValues) Account(android.accounts.Account) SQLException(android.database.SQLException) AccountManager(android.accounts.AccountManager) SQLiteException(android.database.sqlite.SQLiteException) SQLException(android.database.SQLException) OperationApplicationException(android.content.OperationApplicationException)

Example 38 with AccountManager

use of android.accounts.AccountManager in project android by nextcloud.

the class DisplayUtils method setAvatar.

/**
 * fetches and sets the avatar of the given account in the passed callContext
 *
 * @param user        the account to be used to connect to server
 * @param avatarRadius   the avatar radius
 * @param resources      reference for density information
 * @param callContext    which context is called to set the generated avatar
 */
public static void setAvatar(@NonNull User user, AvatarGenerationListener listener, float avatarRadius, Resources resources, Object callContext, Context context) {
    AccountManager accountManager = AccountManager.get(context);
    String userId = accountManager.getUserData(user.toPlatformAccount(), com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
    setAvatar(user, userId, listener, avatarRadius, resources, callContext, context);
}
Also used : AccountManager(android.accounts.AccountManager)

Example 39 with AccountManager

use of android.accounts.AccountManager in project android by nextcloud.

the class SsoGrantPermissionActivity method grantPermission.

private void grantPermission() {
    // create token
    SharedPreferences sharedPreferences = getSharedPreferences(SSO_SHARED_PREFERENCE, Context.MODE_PRIVATE);
    String token = UUID.randomUUID().toString().replaceAll("-", "");
    String hashedTokenWithSalt = EncryptionUtils.generateSHA512(token);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(packageName + DELIMITER + account.name, hashedTokenWithSalt);
    editor.apply();
    String serverUrl;
    String userId;
    try {
        OwnCloudAccount ocAccount = new OwnCloudAccount(account, this);
        serverUrl = ocAccount.getBaseUri().toString();
        AccountManager accountManager = AccountManager.get(this);
        userId = accountManager.getUserData(account, com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
    } catch (AccountUtils.AccountNotFoundException e) {
        Log_OC.e(TAG, "Account not found");
        setResultAndExit(EXCEPTION_ACCOUNT_NOT_FOUND);
        return;
    }
    final Bundle result = new Bundle();
    result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
    result.putString(AccountManager.KEY_ACCOUNT_TYPE, MainApp.getAccountType(this));
    result.putString(AccountManager.KEY_AUTHTOKEN, NEXTCLOUD_SSO);
    result.putString(Constants.SSO_USER_ID, userId);
    result.putString(Constants.SSO_TOKEN, token);
    result.putString(Constants.SSO_SERVER_URL, serverUrl);
    Intent data = new Intent();
    data.putExtra(NEXTCLOUD_SSO, result);
    setResult(RESULT_OK, data);
    finish();
}
Also used : SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) AccountUtils(com.owncloud.android.lib.common.accounts.AccountUtils) AccountManager(android.accounts.AccountManager) Intent(android.content.Intent) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount)

Example 40 with AccountManager

use of android.accounts.AccountManager in project android_packages_apps_Settings by crdroidandroid.

the class MasterClear method loadAccountList.

private void loadAccountList(final UserManager um) {
    View accountsLabel = mContentView.findViewById(R.id.accounts_label);
    LinearLayout contents = (LinearLayout) mContentView.findViewById(R.id.accounts);
    contents.removeAllViews();
    Context context = getActivity();
    final List<UserInfo> profiles = um.getProfiles(UserHandle.myUserId());
    final int profilesSize = profiles.size();
    AccountManager mgr = AccountManager.get(context);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    int accountsCount = 0;
    for (int profileIndex = 0; profileIndex < profilesSize; profileIndex++) {
        final UserInfo userInfo = profiles.get(profileIndex);
        final int profileId = userInfo.id;
        final UserHandle userHandle = new UserHandle(profileId);
        Account[] accounts = mgr.getAccountsAsUser(profileId);
        final int N = accounts.length;
        if (N == 0) {
            continue;
        }
        accountsCount += N;
        AuthenticatorDescription[] descs = AccountManager.get(context).getAuthenticatorTypesAsUser(profileId);
        final int M = descs.length;
        if (profilesSize > 1) {
            View titleView = Utils.inflateCategoryHeader(inflater, contents);
            final TextView titleText = (TextView) titleView.findViewById(android.R.id.title);
            titleText.setText(userInfo.isManagedProfile() ? R.string.category_work : R.string.category_personal);
            contents.addView(titleView);
        }
        for (int i = 0; i < N; i++) {
            Account account = accounts[i];
            AuthenticatorDescription desc = null;
            for (int j = 0; j < M; j++) {
                if (account.type.equals(descs[j].type)) {
                    desc = descs[j];
                    break;
                }
            }
            if (desc == null) {
                Log.w(TAG, "No descriptor for account name=" + account.name + " type=" + account.type);
                continue;
            }
            Drawable icon = null;
            try {
                if (desc.iconId != 0) {
                    Context authContext = context.createPackageContextAsUser(desc.packageName, 0, userHandle);
                    icon = context.getPackageManager().getUserBadgedIcon(authContext.getDrawable(desc.iconId), userHandle);
                }
            } catch (PackageManager.NameNotFoundException e) {
                Log.w(TAG, "Bad package name for account type " + desc.type);
            } catch (Resources.NotFoundException e) {
                Log.w(TAG, "Invalid icon id for account type " + desc.type, e);
            }
            if (icon == null) {
                icon = context.getPackageManager().getDefaultActivityIcon();
            }
            View child = inflater.inflate(R.layout.master_clear_account, contents, false);
            ((ImageView) child.findViewById(android.R.id.icon)).setImageDrawable(icon);
            ((TextView) child.findViewById(android.R.id.title)).setText(account.name);
            contents.addView(child);
        }
    }
    if (accountsCount > 0) {
        accountsLabel.setVisibility(View.VISIBLE);
        contents.setVisibility(View.VISIBLE);
    }
    // Checking for all other users and their profiles if any.
    View otherUsers = mContentView.findViewById(R.id.other_users_present);
    final boolean hasOtherUsers = (um.getUserCount() - profilesSize) > 0;
    otherUsers.setVisibility(hasOtherUsers ? View.VISIBLE : View.GONE);
}
Also used : Context(android.content.Context) Account(android.accounts.Account) Drawable(android.graphics.drawable.Drawable) UserInfo(android.content.pm.UserInfo) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) AuthenticatorDescription(android.accounts.AuthenticatorDescription) PackageManager(android.content.pm.PackageManager) LayoutInflater(android.view.LayoutInflater) UserHandle(android.os.UserHandle) AccountManager(android.accounts.AccountManager) TextView(android.widget.TextView) Resources(android.content.res.Resources) ImageView(android.widget.ImageView) LinearLayout(android.widget.LinearLayout)

Aggregations

AccountManager (android.accounts.AccountManager)180 Account (android.accounts.Account)121 Bundle (android.os.Bundle)28 Intent (android.content.Intent)18 PackageManager (android.content.pm.PackageManager)16 View (android.view.View)16 TextView (android.widget.TextView)16 Context (android.content.Context)15 IOException (java.io.IOException)14 UserAccountManager (com.nextcloud.client.account.UserAccountManager)13 OperationCanceledException (android.accounts.OperationCanceledException)11 ImageView (android.widget.ImageView)9 AuthenticatorDescription (android.accounts.AuthenticatorDescription)8 UserHandle (android.os.UserHandle)8 LayoutInflater (android.view.LayoutInflater)8 AuthenticatorException (android.accounts.AuthenticatorException)7 UserInfo (android.content.pm.UserInfo)7 Resources (android.content.res.Resources)7 Drawable (android.graphics.drawable.Drawable)7 LinearLayout (android.widget.LinearLayout)7