Search in sources :

Example 71 with AccountManager

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

the class FileUploader method onCreate.

/**
     * Service initialization
     */
@Override
public void onCreate() {
    super.onCreate();
    Log_OC.d(TAG, "Creating service");
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    HandlerThread thread = new HandlerThread("FileUploaderThread", Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper, this);
    mBinder = new FileUploaderBinder();
    mUploadsStorageManager = new UploadsStorageManager(getContentResolver());
    int failedCounter = mUploadsStorageManager.failInProgressUploads(// Add UploadResult.KILLED?
    UploadResult.SERVICE_INTERRUPTED);
    if (failedCounter > 0) {
        resurrection();
    }
    // add AccountsUpdatedListener
    AccountManager am = AccountManager.get(getApplicationContext());
    am.addOnAccountsUpdatedListener(this, null, false);
}
Also used : HandlerThread(android.os.HandlerThread) UploadsStorageManager(com.owncloud.android.datamodel.UploadsStorageManager) AccountManager(android.accounts.AccountManager)

Example 72 with AccountManager

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

the class FileActivity method requestCredentialsUpdate.

/**
     * Invalidates the credentials stored for the given OC account and requests new credentials to the user,
     * navigating to {@link AuthenticatorActivity}
     *
     * @param context   Android Context needed to access the {@link AccountManager}. Received as a parameter
     *                  to make the method accessible to {@link android.content.BroadcastReceiver}s.
     * @param account   Stored OC account to request credentials update for. If null, current account will
     *                  be used.
     */
protected void requestCredentialsUpdate(Context context, Account account) {
    try {
        /// step 1 - invalidate credentials of current account
        if (account == null) {
            account = getAccount();
        }
        OwnCloudClient client;
        OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
        client = (OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(ocAccount));
        if (client != null) {
            OwnCloudCredentials cred = client.getCredentials();
            if (cred != null) {
                AccountManager am = AccountManager.get(context);
                if (cred.authTokenExpires()) {
                    am.invalidateAuthToken(account.type, cred.getAuthToken());
                } else {
                    am.clearPassword(account);
                }
            }
        }
        /// step 2 - request credentials to user
        Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
        updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);
        updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivityForResult(updateAccountCredentials, REQUEST_CODE__UPDATE_CREDENTIALS);
    } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
        showSnackMessage(getString(R.string.auth_account_does_not_exist));
    }
}
Also used : AccountUtils(com.owncloud.android.authentication.AccountUtils) AccountManager(android.accounts.AccountManager) Intent(android.content.Intent) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 73 with AccountManager

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

the class UpdateOCVersionOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    AccountManager accountMngr = AccountManager.get(mContext);
    String statUrl = accountMngr.getUserData(mAccount, Constants.KEY_OC_BASE_URL);
    statUrl += AccountUtils.STATUS_PATH;
    RemoteOperationResult result = null;
    GetMethod getMethod = null;
    try {
        getMethod = new GetMethod(statUrl);
        int status = client.executeMethod(getMethod);
        if (status != HttpStatus.SC_OK) {
            result = new RemoteOperationResult(false, getMethod);
            client.exhaustResponse(getMethod.getResponseBodyAsStream());
        } else {
            String response = getMethod.getResponseBodyAsString();
            if (response != null) {
                JSONObject json = new JSONObject(response);
                if (json != null && json.getString("version") != null) {
                    String version = json.getString("version");
                    mOwnCloudVersion = new OwnCloudVersion(version);
                    if (mOwnCloudVersion.isVersionValid()) {
                        accountMngr.setUserData(mAccount, Constants.KEY_OC_VERSION, mOwnCloudVersion.getVersion());
                        Log_OC.d(TAG, "Got new OC version " + mOwnCloudVersion.toString());
                        result = new RemoteOperationResult(ResultCode.OK);
                    } else {
                        Log_OC.w(TAG, "Invalid version number received from server: " + json.getString("version"));
                        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
                    }
                }
            }
            if (result == null) {
                result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
            }
        }
        Log_OC.i(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage());
    } catch (JSONException e) {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
        Log_OC.e(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage(), e);
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Check for update of ownCloud server version at " + client.getWebdavUri() + ": " + result.getLogMessage(), e);
    } finally {
        if (getMethod != null)
            getMethod.releaseConnection();
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONException(org.json.JSONException) AccountManager(android.accounts.AccountManager) OwnCloudVersion(com.owncloud.android.lib.resources.status.OwnCloudVersion) JSONException(org.json.JSONException)

Example 74 with AccountManager

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

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.
     *
     * See {@link com.owncloud.android.authentication.AccountUtils#updateAccountVersion(android.content.Context)}
     *
     * @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());
        String serverUrl, username, oldAccountName, newAccountName;
        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);
                int num = db.update(ProviderTableMeta.FILE_TABLE_NAME, cv, ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", new String[] { oldAccountName });
                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) SQLException(android.database.SQLException) OperationApplicationException(android.content.OperationApplicationException)

Example 75 with AccountManager

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

the class ManageAccountsActivity method createAccount.

@Override
public void createAccount() {
    AccountManager am = AccountManager.get(getApplicationContext());
    am.addAccount(MainApp.getAccountType(), null, null, null, this, new AccountManagerCallback<Bundle>() {

        @Override
        public void run(AccountManagerFuture<Bundle> future) {
            if (future != null) {
                try {
                    Bundle result = future.getResult();
                    String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
                    AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name);
                    mAccountListAdapter = new AccountListAdapter(ManageAccountsActivity.this, getAccountListItems(), mTintedCheck);
                    mListView.setAdapter(mAccountListAdapter);
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            mAccountListAdapter.notifyDataSetChanged();
                        }
                    });
                } catch (OperationCanceledException e) {
                    Log_OC.d(TAG, "Account creation canceled");
                } catch (Exception e) {
                    Log_OC.e(TAG, "Account creation finished in exception: ", e);
                }
            }
        }
    }, mHandler);
}
Also used : Bundle(android.os.Bundle) OperationCanceledException(android.accounts.OperationCanceledException) AccountManager(android.accounts.AccountManager) AccountListAdapter(com.owncloud.android.ui.adapter.AccountListAdapter) OperationCanceledException(android.accounts.OperationCanceledException)

Aggregations

AccountManager (android.accounts.AccountManager)105 Account (android.accounts.Account)76 Bundle (android.os.Bundle)14 IOException (java.io.IOException)11 Intent (android.content.Intent)10 OperationCanceledException (android.accounts.OperationCanceledException)7 AuthenticatorException (android.accounts.AuthenticatorException)5 View (android.view.View)5 TextView (android.widget.TextView)5 HashSet (java.util.HashSet)5 SharedPreferences (android.content.SharedPreferences)4 AccountManagerCallback (android.accounts.AccountManagerCallback)3 Paint (android.graphics.Paint)3 JSONException (org.json.JSONException)3 AccountsException (android.accounts.AccountsException)2 TargetApi (android.annotation.TargetApi)2 AlertDialog (android.app.AlertDialog)2 Context (android.content.Context)2 DialogInterface (android.content.DialogInterface)2 OperationApplicationException (android.content.OperationApplicationException)2