Commit 8a4dcd99 authored by Craig Earley's avatar Craig Earley
Browse files

removes Google Drive code

parent 0e9f836d
......@@ -48,11 +48,6 @@
android:label="Connect to remote db"
android:theme="@style/MainTheme">
</activity>
<activity
android:name=".GoogleDriveFileBrowser"
android:label="Connect to Google Drive"
android:theme="@style/MainTheme">
</activity>
<activity
android:name=".MyDocumentsActivity"
android:label="My Documents"
......
package fieldscience.cs.earlham.edu.fieldday;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
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;
public abstract class GoogleDriveConnect extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient googleApiClient;
private static final int REQUEST_CODE_RESOLUTION = 1;
@Override
protected void onResume(){
super.onResume();
if (googleApiClient == null){
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
googleApiClient.connect();
}
@Override
protected void onPause(){
if (googleApiClient != null){
googleApiClient.disconnect();
}
super.onPause();
}
@Override
public void onConnected(Bundle bundle) { Log.i("GoogleDriveConnect", "Google Drive Connected");}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (IntentSender.SendIntentException e) {
Toast t = Toast.makeText(this, "Could not resolve authorization, sorry.", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
}
} else {
GoogleApiAvailability.getInstance().getErrorDialog(this, connectionResult.getErrorCode(), 0).show();
}
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == REQUEST_CODE_RESOLUTION) {
if (resultCode == RESULT_OK) {
googleApiClient.connect();
}
}
}
@Override
public void onConnectionSuspended(int suspendedCause){ Log.i("GoogleDriveConnect", "Connection Suspended");}
GoogleApiClient getGoogleApiClient() {
return googleApiClient;
}
}
\ No newline at end of file
package fieldscience.cs.earlham.edu.fieldday;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.MetadataChangeSet;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Objects;
public class GoogleDriveFileBrowser extends GoogleDriveConnect {
private DriveResultsAdapter filesAdapter;
private String fileName;
private DriveId folderId;
private static final String TAG = "GoogleDriveFileBrowser";
private static final int REQUEST_CODE = 3;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drive);
ListView filesListView = findViewById(R.id.listFileResults);
filesAdapter = new DriveResultsAdapter(this);
filesListView.setAdapter(filesAdapter);
filesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DriveId driveId = filesAdapter.getItem(position).getDriveId();
switch(filesAdapter.getItem(position).getMimeType()){
case DriveFolder.MIME_TYPE:
retrieveListOfFiles(driveId.asDriveFolder());
break;
case "text/plain":
case "text/html":
openFile(driveId);
break;
}
}
});
fileName = "";
Button fileButton = findViewById(R.id.createFileButton);
fileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
askForName(v);
}
});
Button folderButton = findViewById(R.id.createFolderButton);
folderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
askForName(v);
}
});
}
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
retrieveListOfRootFiles();
}
private void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
private void retrieveListOfRootFiles(){
DriveFolder folder = Drive.DriveApi.getRootFolder(getGoogleApiClient());
Objects.requireNonNull(folder).listChildren(getGoogleApiClient())
.setResultCallback(metadataCallback);
folderId = folder.getDriveId();
}
private void retrieveListOfFiles(DriveFolder folder){
folder.listChildren(getGoogleApiClient())
.setResultCallback(metadataCallback);
folderId = folder.getDriveId();
}
private void openFile(DriveId fileId){
}
private final ResultCallback<DriveFolder.DriveFolderResult> callback = new ResultCallback<DriveFolder.DriveFolderResult>() {
@Override
public void onResult(DriveFolder.DriveFolderResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create the folder");
return;
}
showMessage("Created a folder: " + result.getDriveFolder().getDriveId());
}
};
private void askForName(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(GoogleDriveFileBrowser.this);
final EditText name = new EditText(this);
// name.setTextColor(getResources().getColor(R.color.text_green));
builder.setView(name);
builder.setNegativeButton("Cancel", null);
switch(v.getId()){
case R.id.createFolderButton:
builder.setMessage("Enter folder name");
builder.setPositiveButton("Create Folder", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createFolder(name.getText().toString());
}
});
break;
case R.id.createFileButton:
builder.setMessage("Enter file name");
builder.setPositiveButton("Create File", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createFile(name.getText().toString());
}
});
break;
}
builder.create().show();
}
private void createFolder(String folderName) {
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(folderName).build();
Objects.requireNonNull(Drive.DriveApi.getRootFolder(getGoogleApiClient())).createFolder(
getGoogleApiClient(), changeSet).setResultCallback(callback);
retrieveListOfRootFiles();
}
private void createFile(String fileName) {
Drive.DriveApi.newDriveContents(getGoogleApiClient())
.setResultCallback(driveContentsCallback);
this.fileName = fileName;
}
final private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback = new
ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Problem while retrieving files");
return;
}
filesAdapter.clear();
filesAdapter.append(result.getMetadataBuffer());
showMessage("Successfully listed files.");
}
};
final private ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback = new
ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create new file contents");
return;
}
final DriveContents driveContents = result.getDriveContents();
// Perform I/O off the UI thread.
new Thread() {
@Override
public void run() {
// write content to DriveContents
OutputStream outputStream = driveContents.getOutputStream();
Writer writer = new OutputStreamWriter(outputStream);
try {
writer.write("Hello World!");
writer.close();
} catch (IOException e) {
Log.e(TAG, Objects.requireNonNull(e.getMessage()));
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(fileName)
.setMimeType("text/plain")
.setStarred(true).build();
DriveFolder folder = folderId.asDriveFolder();
folder.createFile(getGoogleApiClient(), changeSet, driveContents);
retrieveListOfFiles(folder);
}
}.start();
}
};
final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
ResultCallback<DriveFolder.DriveFileResult>() {
@Override
public void onResult(DriveFolder.DriveFileResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create the file");
return;
}
showMessage("Created a file with content: " + result.getDriveFile().getDriveId());
}
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment