Search in sources :

Example 1 with GoogleApiClient

use of com.google.android.gms.common.api.GoogleApiClient in project SeriesGuide by UweTrottmann.

the class HexagonTools method checkSignInState.

private void checkSignInState() {
    if (credential.getSelectedAccount() != null && !isTimeForSignInStateCheck()) {
        Timber.d("%s: just checked state, skip", ACTION_SILENT_SIGN_IN);
    }
    lastSignInCheck = SystemClock.elapsedRealtime();
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(app).addApi(Auth.GOOGLE_SIGN_IN_API, getGoogleSignInOptions()).build();
    }
    android.accounts.Account account = null;
    ConnectionResult connectionResult = googleApiClient.blockingConnect();
    if (connectionResult.isSuccess()) {
        OptionalPendingResult<GoogleSignInResult> pendingResult = Auth.GoogleSignInApi.silentSignIn(googleApiClient);
        GoogleSignInResult result = pendingResult.await();
        if (result.isSuccess()) {
            GoogleSignInAccount signInAccount = result.getSignInAccount();
            if (signInAccount != null) {
                Timber.i("%s: successful", ACTION_SILENT_SIGN_IN);
                account = signInAccount.getAccount();
                credential.setSelectedAccount(account);
            } else {
                trackSignInFailure(ACTION_SILENT_SIGN_IN, "GoogleSignInAccount is null");
            }
        } else {
            trackSignInFailure(ACTION_SILENT_SIGN_IN, result.getStatus());
        }
        googleApiClient.disconnect();
    } else {
        trackSignInFailure(ACTION_SILENT_SIGN_IN, connectionResult);
    }
    boolean shouldFixAccount = account == null;
    PreferenceManager.getDefaultSharedPreferences(app).edit().putBoolean(HexagonSettings.KEY_SHOULD_VALIDATE_ACCOUNT, shouldFixAccount).apply();
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) ConnectionResult(com.google.android.gms.common.ConnectionResult) GoogleSignInResult(com.google.android.gms.auth.api.signin.GoogleSignInResult)

Example 2 with GoogleApiClient

use of com.google.android.gms.common.api.GoogleApiClient in project muzei by romannurik.

the class ArtworkCacheIntentService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).build();
    ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
    if (!connectionResult.isSuccess()) {
        Log.e(TAG, "Failed to connect to GoogleApiClient: " + connectionResult.getErrorCode());
        return;
    }
    // Read all DataItems
    DataItemBuffer dataItemBuffer = Wearable.DataApi.getDataItems(googleApiClient).await();
    if (!dataItemBuffer.getStatus().isSuccess()) {
        Log.e(TAG, "Error getting all data items: " + dataItemBuffer.getStatus().getStatusMessage());
    }
    Iterator<DataItem> dataItemIterator = dataItemBuffer.singleRefIterator();
    boolean foundArtwork = false;
    while (dataItemIterator.hasNext()) {
        DataItem dataItem = dataItemIterator.next();
        foundArtwork = foundArtwork || processDataItem(googleApiClient, dataItem);
    }
    dataItemBuffer.release();
    if (foundArtwork) {
        // Enable the Full Screen Activity and Artwork Complication Provider Service only if we've found artwork
        enableComponents(FullScreenActivity.class, ArtworkComplicationProviderService.class);
    }
    if (!foundArtwork && intent != null && intent.getBooleanExtra(SHOW_ACTIVATE_NOTIFICATION_EXTRA, false)) {
        ActivateMuzeiIntentService.maybeShowActivateMuzeiNotification(this);
    } else {
        ActivateMuzeiIntentService.clearNotifications(this);
    }
    googleApiClient.disconnect();
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) DataItem(com.google.android.gms.wearable.DataItem) ConnectionResult(com.google.android.gms.common.ConnectionResult) DataItemBuffer(com.google.android.gms.wearable.DataItemBuffer)

Example 3 with GoogleApiClient

use of com.google.android.gms.common.api.GoogleApiClient in project muzei by romannurik.

the class WearableController method updateArtwork.

public static synchronized void updateArtwork(Context context) {
    if (ConnectionResult.SUCCESS != GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)) {
        return;
    }
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(Wearable.API).build();
    ConnectionResult connectionResult = googleApiClient.blockingConnect(5, TimeUnit.SECONDS);
    if (!connectionResult.isSuccess()) {
        if (connectionResult.getErrorCode() != ConnectionResult.API_UNAVAILABLE) {
            Log.w(TAG, "onConnectionFailed: " + connectionResult);
        }
        return;
    }
    ContentResolver contentResolver = context.getContentResolver();
    Bitmap image = null;
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(contentResolver.openInputStream(MuzeiContract.Artwork.CONTENT_URI), null, options);
        options.inJustDecodeBounds = false;
        if (options.outWidth > options.outHeight) {
            options.inSampleSize = ImageUtil.calculateSampleSize(options.outHeight, 320);
        } else {
            options.inSampleSize = ImageUtil.calculateSampleSize(options.outWidth, 320);
        }
        image = BitmapFactory.decodeStream(contentResolver.openInputStream(MuzeiContract.Artwork.CONTENT_URI), null, options);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Unable to read artwork to update Android Wear", e);
    }
    if (image != null) {
        int rotation = getRotation(context);
        if (rotation != 0) {
            // Rotate the image so that Wear always gets a right side up image
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
        }
        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
        Asset asset = Asset.createFromBytes(byteStream.toByteArray());
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/artwork");
        Artwork artwork = MuzeiContract.Artwork.getCurrentArtwork(context);
        dataMapRequest.getDataMap().putDataMap("artwork", DataMap.fromBundle(artwork.toBundle()));
        dataMapRequest.getDataMap().putAsset("image", asset);
        Wearable.DataApi.putDataItem(googleApiClient, dataMapRequest.asPutDataRequest().setUrgent()).await();
    }
    googleApiClient.disconnect();
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) Artwork(com.google.android.apps.muzei.api.Artwork) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ContentResolver(android.content.ContentResolver) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) ConnectionResult(com.google.android.gms.common.ConnectionResult) Asset(com.google.android.gms.wearable.Asset) BitmapFactory(android.graphics.BitmapFactory) PutDataMapRequest(com.google.android.gms.wearable.PutDataMapRequest)

Example 4 with GoogleApiClient

use of com.google.android.gms.common.api.GoogleApiClient in project google-services by googlesamples.

the class MainActivity method onCreate.

// [END resolution_variables]
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // [START restore_saved_instance_state]
    if (savedInstanceState != null) {
        mIsResolving = savedInstanceState.getBoolean(KEY_IS_RESOLVING);
        mShouldResolve = savedInstanceState.getBoolean(KEY_SHOULD_RESOLVE);
    }
    // [END restore_saved_instance_state]
    // Set up button click listeners
    findViewById(R.id.sign_in_button).setOnClickListener(this);
    findViewById(R.id.sign_out_button).setOnClickListener(this);
    findViewById(R.id.disconnect_button).setOnClickListener(this);
    // Large sign-in
    ((SignInButton) findViewById(R.id.sign_in_button)).setSize(SignInButton.SIZE_WIDE);
    // Start with sign-in button disabled until sign-in either succeeds or fails
    findViewById(R.id.sign_in_button).setEnabled(false);
    // Set up view instances
    mStatus = (TextView) findViewById(R.id.status);
    // [START create_google_api_client]
    // Build GoogleApiClient with access to basic profile
    mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API).addScope(new Scope(Scopes.PROFILE)).addScope(new Scope(Scopes.EMAIL)).build();
// [END create_google_api_client]
}
Also used : SignInButton(com.google.android.gms.common.SignInButton) GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) Scope(com.google.android.gms.common.api.Scope)

Example 5 with GoogleApiClient

use of com.google.android.gms.common.api.GoogleApiClient in project FirebaseUI-Android by firebase.

the class GoogleApiClientTaskHelper method getConnectedGoogleApiClient.

public Task<GoogleApiClient> getConnectedGoogleApiClient() {
    final TaskCompletionSource<GoogleApiClient> source = new TaskCompletionSource<>();
    if (!mConnectTaskRef.compareAndSet(null, source)) {
        // mConnectTaskRef Task was not null, return Task
        return mConnectTaskRef.get().getTask();
    }
    final GoogleApiClient client = mBuilder.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {

        @Override
        public void onConnected(@Nullable Bundle bundle) {
            source.setResult(mClientRef.get());
            if (mClientRef.get() != null) {
                mClientRef.get().unregisterConnectionCallbacks(this);
            }
        }

        @Override
        public void onConnectionSuspended(int i) {
        }
    }).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {

        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
            source.setException(new IOException("Failed to connect GoogleApiClient: " + connectionResult.getErrorMessage()));
            if (mClientRef.get() != null) {
                mClientRef.get().unregisterConnectionFailedListener(this);
            }
        }
    }).build();
    mClientRef.set(client);
    client.connect();
    return source.getTask();
}
Also used : TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) Bundle(android.os.Bundle) NonNull(android.support.annotation.NonNull) ConnectionResult(com.google.android.gms.common.ConnectionResult) IOException(java.io.IOException)

Aggregations

GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)33 ConnectionResult (com.google.android.gms.common.ConnectionResult)10 Intent (android.content.Intent)6 Bundle (android.os.Bundle)6 Handler (android.os.Handler)4 PendingIntent (android.app.PendingIntent)3 SharedPreferences (android.content.SharedPreferences)3 Bitmap (android.graphics.Bitmap)3 NonNull (android.support.annotation.NonNull)3 GoogleSignInOptions (com.google.android.gms.auth.api.signin.GoogleSignInOptions)3 NotificationManager (android.app.NotificationManager)2 IntentFilter (android.content.IntentFilter)2 ColorDrawable (android.graphics.drawable.ColorDrawable)2 NotificationCompat (android.support.v4.app.NotificationCompat)2 Toolbar (android.support.v7.widget.Toolbar)2 ConnectivityReceiver (com.applozic.mobicomkit.broadcast.ConnectivityReceiver)2 AlCustomizationSettings (com.applozic.mobicomkit.uiwidgets.AlCustomizationSettings)2 ApplozicPermissions (com.applozic.mobicomkit.uiwidgets.instruction.ApplozicPermissions)2 Status (com.google.android.gms.common.api.Status)2 DataItem (com.google.android.gms.wearable.DataItem)2