@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main22);
    ed = (EditText) findViewById(R.id.editText2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    googleApiClient = MainActivity.getGoogleApiClient();
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    id = DriveId.decodeFromString(getIntent().getExtras().getString("id"));
    text = "";
    final DriveFile df = id.asDriveFile();
    final Lectura llig = new Lectura();

    llig.execute(df);

    while (!isReaded) {
      if (text == "") {

      } else {
        ed.setText(text);
        isReaded = true;
      }
    }
    final Escritura escriu = new Escritura();

    fab.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Actualitzant...", Toast.LENGTH_SHORT).show();
            escriu.setText(ed.getText().toString());

            escriu.execute(df);

            Toast.makeText(getApplicationContext(), "Actualitzat!", Toast.LENGTH_SHORT).show();
            finishActivity(RESULT_OK);
          }
        });

    ResultCallback<DriveApi.DriveContentsResult> contentsOpenedCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
          @Override
          public void onResult(DriveApi.DriveContentsResult result) {
            if (!result.getStatus().isSuccess()) {

              // display an error saying file can't be opened
              return;
            }
            // DriveContents object contains pointers
            // to the actual byte stream
            DriveContents contents = result.getDriveContents();
          }
        };

    PendingResult<DriveApi.DriveContentsResult> opened =
        df.open(googleApiClient, DriveFile.MODE_READ_WRITE, null);
    opened.setResultCallback(contentsOpenedCallback);
  }
 @Override
 public void onResult(DriveApi.DriveIdResult driveIdResult) {
   if (!driveIdResult.getStatus().isSuccess()) {
     showMessage("Cannot find driveId. Are you authorized to view this file");
     return;
   }
   DriveFile file =
       Drive.DriveApi.getFile(getmGoogleApiClient(), driveIdResult.getDriveId());
   file.getDriveId().encodeToString();
   file.getMetadata(getmGoogleApiClient()).setResultCallback(mMetadataCallback);
 }
  private void upload_write(
      DriveFile file,
      final String oldData,
      final String newData,
      final GenericCallback<String> resultCallback) {
    file.open(
            client,
            DriveFile.MODE_WRITE_ONLY,
            new DownloadProgressListener() {

              @Override
              public void onProgress(long bytesDownloaded, long bytesExpected) {
                System.out.println(bytesDownloaded + " / " + bytesExpected);
              }
            })
        .setResultCallback(
            new ResultCallback<DriveApi.DriveContentsResult>() {

              @Override
              public void onResult(DriveContentsResult result) {
                DriveContents contents = result.getDriveContents();

                // Output
                OutputStream out = contents.getOutputStream();

                try {
                  // Resove conflicts
                  ConflictResolver resolver = new ConflictResolver();
                  String resolved = resolver.resolveConflicts(oldData, newData);

                  // Write
                  out.write(resolved.getBytes());
                  out.flush();
                  out.close();

                  Log.d(TAG, "File contents (after) = " + resolved);

                  contents
                      .commit(client, null)
                      .setResultCallback(
                          new ResultCallback<Status>() {

                            @Override
                            public void onResult(Status status) {
                              String str = status.isSuccess() ? "Bookmarks.dap updated" : "Failure";
                              Log.d(TAG, "Status: " + str);
                              resultCallback.onResult(str);
                            }
                          });
                } catch (IOException e) {
                  Log.d(TAG, "Failed to read/write contents");
                  e.printStackTrace();
                }
              }
            });
  }
  private void common_read(
      final DriveFile file,
      final String newData,
      final Mode mode,
      final GenericCallback<String> resultCallback) {
    file.open(
            client,
            DriveFile.MODE_READ_ONLY,
            new DownloadProgressListener() {

              @Override
              public void onProgress(long bytesDownloaded, long bytesExpected) {
                System.out.println(bytesDownloaded + " / " + bytesExpected);
              }
            })
        .setResultCallback(
            new ResultCallback<DriveApi.DriveContentsResult>() {

              @Override
              public void onResult(DriveContentsResult result) {
                System.out.println("Status: " + result.getStatus().toString());
                DriveContents contents = result.getDriveContents();
                if (contents == null) System.out.println("---------- Contents is null ---------");

                // Input
                InputStream stream = contents.getInputStream();
                InputStreamReader reader = new InputStreamReader(stream);
                BufferedReader in = new BufferedReader(reader);

                try {
                  // Read
                  StringBuilder stringbuilder = new StringBuilder();
                  String line = in.readLine();
                  while (line != null) {
                    stringbuilder.append(line);
                    line = in.readLine();
                  }
                  String oldData = stringbuilder.toString();
                  in.close();
                  Log.d(TAG, "File contents (before) = " + oldData);

                  switch (mode) {
                    case DOWNLOAD:
                      resultCallback.onResult(oldData);
                      break;
                    case UPLOAD:
                      upload_write(file, oldData, newData, resultCallback);
                  }

                } catch (IOException e) {
                  Log.d(TAG, "Failed to read/write contents");
                  e.printStackTrace();
                }
              }
            });
  }
 public void update(
     final FormTemplate formToSave,
     final GoogleApiClient googleClient,
     final DriveId fileDriveId,
     final String fileName) {
   DriveFile driveFile = Drive.DriveApi.getFile(googleClient, fileDriveId);
   driveFile
       .open(googleClient, DriveFile.MODE_WRITE_ONLY, null)
       .setResultCallback(
           new ResultCallback<DriveApi.DriveContentsResult>() {
             public void onResult(DriveApi.DriveContentsResult result) {
               Persister persister = new Persister();
               try {
                 persister.write(formToSave, result.getDriveContents().getOutputStream());
               } catch (Exception ex) {
                 Log.e(MainActivity.PT_APP_INFO, "Cannot save for to google drive because " + ex);
               }
             }
           });
 }