Created
April 22, 2016 18:16
-
-
Save paolorotolo/d0ff147ad28af582829bebf3b9e071b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2016 Glucosio Foundation | |
* | |
* This file is part of Glucosio. | |
* | |
* Glucosio is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, version 3. | |
* | |
* Glucosio is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with Glucosio. If not, see <http://www.gnu.org/licenses/>. | |
* | |
* | |
*/ | |
import android.app.Activity; | |
import android.content.IntentSender; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.util.Log; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GoogleApiAvailability; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.drive.Drive; | |
import java.lang.ref.WeakReference; | |
public class GoogleDriveBackup implements Backup, GoogleApiClient.OnConnectionFailedListener { | |
@Nullable | |
private GoogleApiClient googleApiClient; | |
@Nullable | |
private WeakReference<Activity> activityRef; | |
@Override | |
public void init(@NonNull final Activity activity) { | |
this.activityRef = new WeakReference<>(activity); | |
googleApiClient = new GoogleApiClient.Builder(activity) | |
.addApi(Drive.API) | |
.addScope(Drive.SCOPE_FILE) | |
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { | |
@Override | |
public void onConnected(Bundle bundle) { | |
// Do nothing | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
} | |
}) | |
.addOnConnectionFailedListener(this) | |
.build(); | |
} | |
@Override | |
public GoogleApiClient getClient(){ | |
return googleApiClient; | |
} | |
@Override | |
public void start() { | |
if (googleApiClient != null) { | |
googleApiClient.connect(); | |
} else { | |
throw new IllegalStateException("You should call init before start"); | |
} | |
} | |
@Override | |
public void stop() { | |
if (googleApiClient != null) { | |
googleApiClient.disconnect(); | |
} else { | |
throw new IllegalStateException("You should call init before start"); | |
} | |
} | |
@Override | |
public void onConnectionFailed(@NonNull final ConnectionResult result) { | |
Log.i("Connection Failed", "GoogleApiClient connection failed: " + result.toString()); | |
if (result.hasResolution() && activityRef != null && activityRef.get() != null) { | |
Activity a = activityRef.get(); | |
// show the localized error dialog. | |
try { | |
result.startResolutionForResult(a, 1); | |
} catch (IntentSender.SendIntentException e) { | |
e.printStackTrace(); | |
GoogleApiAvailability.getInstance().getErrorDialog(a, result.getErrorCode(), 0).show(); | |
} | |
} else { | |
Log.d("error", "cannot resolve connection issue"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment