Example usage for android.content Context ACCOUNT_SERVICE

List of usage examples for android.content Context ACCOUNT_SERVICE

Introduction

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

Prototype

String ACCOUNT_SERVICE

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

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.accounts.AccountManager for receiving intents at a time of your choosing.

Usage

From source file:io.v.android.apps.syncslides.SignInActivity.java

private void fetchUserProfile() {
    if (!FETCH_PROFILE) {
        fetchUserProfileDone(null);/*  ww  w .  j av  a2s .  c  o  m*/
        return;
    }
    AccountManager manager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
    Account[] accounts = manager.getAccountsByType("com.google");
    Account account = null;
    for (int i = 0; i < accounts.length; i++) {
        if (accounts[i].name.equals(mAccountName)) {
            account = accounts[i];
            break;
        }
    }
    if (account == null) {
        Log.e(TAG, "Couldn't find Google account with name: " + mAccountName);
        pickAccount();
        return;
    }
    manager.getAuthToken(account, OAUTH_SCOPE, new Bundle(), false, new OnTokenAcquired(),
            new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {
                    Log.e(TAG, "Error getting auth token: " + msg.toString());
                    fetchUserProfileDone(null);
                    return true;
                }
            }));
}

From source file:com.owncloud.android.ui.activity.ReceiveExternalFilesActivity.java

@Override
protected void setAccount(Account account, boolean savedAccount) {
    if (somethingToUpload()) {
        mAccountManager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
        Account[] accounts = mAccountManager.getAccountsByType(MainApp.getAccountType());
        if (accounts.length == 0) {
            Log_OC.i(TAG, "No ownCloud account is available");
            showDialog(DIALOG_NO_ACCOUNT);
        } else if (accounts.length > 1 && !mAccountSelected && !mAccountSelectionShowing) {
            Log_OC.i(TAG, "More than one ownCloud is available");
            showDialog(DIALOG_MULTIPLE_ACCOUNT);
            mAccountSelectionShowing = true;
        } else {/*from  w ww . j  a  v a 2  s.c o  m*/
            if (!savedAccount) {
                setAccount(accounts[0]);
            }
        }
    } else {
        showErrorDialog(R.string.uploader_error_message_no_file_to_upload,
                R.string.uploader_error_title_no_file_to_upload);
    }

    super.setAccount(account, savedAccount);
}

From source file:com.beesham.popularmovies.sync.MoviesSyncAdapter.java

private static Account getSyncAccount(Context context) {
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));

    if (accountManager.getPassword(newAccount) == null) {
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }/*ww w .j ava2  s . c om*/
        onAccountCreated(newAccount, context);
    }
    return newAccount;
}

From source file:p1.nd.khan.jubair.mohammadd.popularmovies.sync.MovieSyncAdapter.java

/**
 * Helper method to get the fake account to be used with SyncAdapter, or make a new one
 * if the fake account doesn't exist yet.  If we make a new account, we call the
 * onAccountCreated method so we can initialize things.
 *
 * @param context  The context used to access the account service
 * @return a fake account.//from   w w w . j a v  a 2  s  . co  m
 */
public static Account getSyncAccount(Context context) {
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));
    if (null == accountManager.getPassword(newAccount)) {
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }
        onAccountCreated(newAccount, context);
    }
    return newAccount;
}

From source file:eu.nubomedia.nubomedia_kurento_health_communicator_android.kc_and_client.ui.activity.ItemListActivity.java

@Override
protected void onResume() {
    super.onResume();

    if (AppUtils.getDefaults(ConstantKeys.FROMLOGIN, this).equalsIgnoreCase("true")) {
        finish();//  w ww.  jav  a 2s. com
    }

    AppUtils.CancelNotification(getApplicationContext());

    try {
        Account ac = AccountUtils.getAccount(this, true);
        if (ac != null) {
            account = ac;
            AccountManager am = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
            String userName = am.getUserData(ac, JsonKeys.NAME) + " " + am.getUserData(ac, JsonKeys.SURNAME);
            getActionBar().setSubtitle(userName);
        }
    } catch (Exception e) {
        Log.d("Error trying to get user data", e.toString());
    }

    // registering our receiver
    registerReceiver(mGCMReceiver, gcmFilter);
    refreshList();
}

From source file:com.example.igorklimov.popularmoviesdemo.sync.SyncAdapter.java

/**
 * Create a new dummy account for the sync adapter
 *
 * @param context The application context
 */// w ww  .j  av a  2 s .  co m
public static Account getSyncAccount(Context context) {
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.acc_sync_type));
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    if (accountManager.getPassword(newAccount) == null) {
        if (!accountManager.addAccountExplicitly(newAccount, "", null))
            return null;

        onAccountCreated(newAccount, context);
    }

    return newAccount;
}

From source file:eu.nubomedia.nubomedia_kurento_health_communicator_android.kc_and_communicator.util.FileUtils.java

public static void DownloadFromUrl(final String media, final String messageId, final Context ctx,
        final ImageView container, final Object object, final String timelineId, final String localId,
        final Long fileSize) {

    new AsyncTask<Void, Void, Boolean>() {
        private boolean retry = true;
        private Bitmap imageDownloaded;
        private BroadcastReceiver mDownloadCancelReceiver;
        private HttpGet job;
        private AccountManager am;
        private Account account;
        private String authToken;

        @Override//from  w w  w .j  a  v  a  2  s  .c  o m
        protected void onPreExecute() {
            IntentFilter downloadFilter = new IntentFilter(ConstantKeys.BROADCAST_CANCEL_PROCESS);
            mDownloadCancelReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String localIdToClose = (String) intent.getExtras().get(ConstantKeys.LOCALID);
                    if (localId.equals(localIdToClose)) {
                        try {
                            job.abort();
                        } catch (Exception e) {
                            log.debug("The process was canceled");
                        }
                        cancel(false);
                    }
                }
            };

            // registering our receiver
            ctx.getApplicationContext().registerReceiver(mDownloadCancelReceiver, downloadFilter);
        }

        @Override
        protected void onCancelled() {
            File file1 = new File(FileUtils.getDir(), localId + ConstantKeys.EXTENSION_JPG);
            File file2 = new File(FileUtils.getDir(), localId + ConstantKeys.EXTENSION_3GP);

            if (file1.exists()) {
                file1.delete();
            }
            if (file2.exists()) {
                file2.delete();
            }

            file1 = null;
            file2 = null;
            System.gc();
            try {
                ctx.getApplicationContext().unregisterReceiver(mDownloadCancelReceiver);
            } catch (Exception e) {
                log.debug("Receriver unregister from another code");
            }

            for (int i = 0; i < AppUtils.getlistOfDownload().size(); i++) {
                if (AppUtils.getlistOfDownload().get(i).equals(localId)) {
                    AppUtils.getlistOfDownload().remove(i);
                }
            }

            DataBasesAccess.getInstance(ctx.getApplicationContext()).MessagesDataBaseWriteTotal(localId, 100);

            Intent intent = new Intent();
            intent.setAction(ConstantKeys.BROADCAST_DIALOG_DOWNLOAD_FINISH);
            intent.putExtra(ConstantKeys.LOCALID, localId);
            ctx.sendBroadcast(intent);

            if (object != null) {
                ((ProgressDialog) object).dismiss();
            }
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                File file1 = new File(FileUtils.getDir(), localId + ConstantKeys.EXTENSION_JPG);
                File file2 = new File(FileUtils.getDir(), localId + ConstantKeys.EXTENSION_3GP);
                // firt we are goint to search the local files
                if ((!file1.exists()) && (!file2.exists())) {
                    account = AccountUtils.getAccount(ctx.getApplicationContext(), false);
                    am = (AccountManager) ctx.getSystemService(Context.ACCOUNT_SERVICE);
                    authToken = ConstantKeys.STRING_DEFAULT;
                    authToken = am.blockingGetAuthToken(account, ctx.getString(R.string.account_type), true);

                    MessagingClientService messageService = new MessagingClientService(
                            ctx.getApplicationContext());

                    URL urlObj = new URL(Preferences.getServerProtocol(ctx), Preferences.getServerAddress(ctx),
                            Preferences.getServerPort(ctx), ctx.getString(R.string.url_get_content));

                    String url = ConstantKeys.STRING_DEFAULT;
                    url = Uri.parse(urlObj.toString()).buildUpon().build().toString() + timelineId + "/"
                            + messageId + "/" + "content";

                    job = new HttpGet(url);
                    // first, get free space
                    FreeUpSpace(ctx, fileSize);
                    messageService.getContent(authToken, media, messageId, timelineId, localId, false, false,
                            fileSize, job);
                }

                if (file1.exists()) {
                    imageDownloaded = decodeSampledBitmapFromPath(file1.getAbsolutePath(), 200, 200);
                } else if (file2.exists()) {
                    imageDownloaded = ThumbnailUtils.createVideoThumbnail(file2.getAbsolutePath(),
                            MediaStore.Images.Thumbnails.MINI_KIND);
                }

                if (imageDownloaded == null) {
                    return false;
                }
                return true;
            } catch (Exception e) {
                deleteFiles();
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean result) {
            // We have the media
            try {
                ctx.getApplicationContext().unregisterReceiver(mDownloadCancelReceiver);
            } catch (Exception e) {
                log.debug("Receiver was closed on cancel");
            }

            if (!(localId.contains(ConstantKeys.AVATAR))) {
                for (int i = 0; i < AppUtils.getlistOfDownload().size(); i++) {
                    if (AppUtils.getlistOfDownload().get(i).equals(localId)) {
                        AppUtils.getlistOfDownload().remove(i);
                    }
                }

                DataBasesAccess.getInstance(ctx.getApplicationContext()).MessagesDataBaseWriteTotal(localId,
                        100);

                Intent intent = new Intent();
                intent.setAction(ConstantKeys.BROADCAST_DIALOG_DOWNLOAD_FINISH);
                intent.putExtra(ConstantKeys.LOCALID, localId);
                ctx.sendBroadcast(intent);
            }

            if (object != null) {
                ((ProgressDialog) object).dismiss();
            }

            // Now the only container could be the avatar in edit screen
            if (container != null) {
                if (imageDownloaded != null) {
                    container.setImageBitmap(imageDownloaded);
                } else {
                    deleteFiles();
                    imageDownloaded = decodeSampledBitmapFromResource(ctx.getResources(),
                            R.drawable.ic_error_loading, 200, 200);
                    container.setImageBitmap(imageDownloaded);
                    Toast.makeText(ctx.getApplicationContext(),
                            ctx.getApplicationContext().getText(R.string.donwload_fail), Toast.LENGTH_SHORT)
                            .show();
                }
            } else {
                showMedia(localId, ctx, (ProgressDialog) object);
            }

        }

        private void deleteFiles() {
            File file1 = new File(FileUtils.getDir(), localId + ConstantKeys.EXTENSION_JPG);
            File file2 = new File(FileUtils.getDir(), localId + ConstantKeys.EXTENSION_3GP);
            if (file1.exists()) {
                file1.delete();
            }
            if (file2.exists()) {
                file2.delete();
            }
        }

    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

From source file:com.creationgroundmedia.popularmovies.sync.MovieSyncAdapter.java

public static Account getSyncAccount(Context context) {
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    // Create the account type and default account
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.sync_account_type));

    // If the password doesn't exist, the account doesn't exist
    if (null == accountManager.getPassword(newAccount)) {

        /*/*from   w ww.  j a  va2 s.co  m*/
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
            return null;
        }
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */
        onAccountCreated(newAccount, context);
    }
    return newAccount;
}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static String getDeviceSalt(Context context) {
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    Account account = getCMAccountAccount(context);
    return getDeviceSalt(accountManager, account);
}

From source file:com.phonemetra.account.util.AccountUtils.java

public static String getDeviceSalt(Context context) {
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    Account account = getAccountAccount(context);
    return getDeviceSalt(accountManager, account);
}