title: CloudRail implementation of GoogleDrive
body: <p>Is there any CloudRail sample only for GoogleDrive upload/download? I followed <a href="https://www.youtube.com/watch?v=urDM-HoEr9w&amp;feature=youtu.be" rel="nofollow noreferrer">this video</a> and <a href="https://blog.cloudrail.com/authenticating-google-drive/" rel="nofollow noreferrer">this tuorial</a> and managed to make it working for authentication (Really great tutorials, even for newbies). Then I tried to use <a href="https://github.com/CloudRail/cloudrail-si-android-sdk/tree/master/ExampleProjects/UnifiedCloudStorage" rel="nofollow noreferrer">github sample</a> for cloud storage, it is working when using the github project but when I try to integrate to my app then the app is not getting CloudRail initial screen and stopping suddenly.</p>
  
  <p>Also the github project is old and using deprecated methods and API for example</p>
  
  <p><strong>setDrawerListener,</strong></p>
  
  <p><strong><a href="https://developer.android.com/reference/android/app/Fragment" rel="nofollow noreferrer">Fragment</a></strong> as per android documentation</p>
  
  <p>It would be great if the CouldRail team provide more tutorials like that.</p>
  
  <p><strong>EDIT-</strong> below is the source code</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package com.mpathak.driverail;
  
  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  
  import com.cloudrail.si.CloudRail;
  
  public class MainActivity extends AppCompatActivity {
  
      private static final String BROWSABLE = "android.intent.category.BROWSABLE";
      FileInputStream fileInputStream = null;
      File image = new File(Environment.getExternalStorageDirectory(),"/DCIM/Camera/someImage.jpg");
      CloudStorage googleDriveService = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
         int Service_Number = 1;
         Services.getInstance().prepare(this);
          googleDriveService = Services.getInstance().getService(Service_Number);
  
          new Thread(new Runnable() {
              @Override
              public void run() {
                  try {
                      fileInputStream = new FileInputStream(image);
                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                  }
  
                  String imagesavingPath = "/" + image.getName();
  
                  if (fileInputStream != null)
                  {
                      googleDriveService.upload(imagesavingPath, fileInputStream, image.length(), true);
                  }
                  fileInputStream = null;
              }
          });
  
          setContentView(R.layout.activity_main);
      }
  
      @Override
      protected void onNewIntent(Intent intent)
      {
          if(intent.getCategories().contains(BROWSABLE)){
              CloudRail.setAuthenticationResponse(intent);
          }
  
          super.onNewIntent(intent);
      }
  
      @Override
      protected void onStop() {
          Services.getInstance().storePersistent();
          super.onStop();
      }
  
  
  }
  </code></pre>
  
  <p><strong>Services.java</strong></p>
  
  <pre><code>package com.mpathak.driverail;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.SharedPreferences;
  
  import com.cloudrail.si.CloudRail;
  import com.cloudrail.si.exceptions.ParseException;
  import com.cloudrail.si.interfaces.CloudStorage;
  import com.cloudrail.si.services.GoogleDrive;
  
  import java.util.concurrent.atomic.AtomicReference;
  
  /**
   * This class encapsulates the different services being used by the application. It also initializes
   * them and persists the authentication data.
   *
   */
  public class Services {
      private final static String CLOUDRAIL_LICENSE_KEY = "MY_CLOUDRAIL_KEY";
      private final static Services ourInstance = new Services();
  
      private final AtomicReference&lt;CloudStorage&gt; googledrive = new AtomicReference&lt;&gt;();
  
      private Activity context = null;
  
      static Services getInstance() {
          return ourInstance;
      }
  
      private Services() {
      }
  
      private void initGoogleDrive() {
          googledrive.set(new GoogleDrive(context, "MY_GOOGLE_ID",
                  "", "com.mpathak.driverail:/oauth2redirect", ""));
  
          try
          {
              ((GoogleDrive) googledrive.get()).useAdvancedAuthentication();
          }
          catch(Exception ex) {
  
          }
      }
  
      // --------- Public Methods -----------
      void prepare(Activity context) {
          this.context = context;
  
          CloudRail.setAppKey(CLOUDRAIL_LICENSE_KEY);
  
          this.initGoogleDrive();
  
          SharedPreferences sharedPreferences = context.getPreferences(Context.MODE_PRIVATE);
  
          try {
              String persistent = sharedPreferences.getString("googledrivePersistent", null);
              if (persistent != null)
              {
                  googledrive.get().loadAsString(persistent);
              }
  
          } catch (ParseException e) {}
      }
  
      CloudStorage getService(int service) {
          AtomicReference&lt;CloudStorage&gt; ret = new AtomicReference&lt;&gt;();
  
          switch (service) {
              case 1:
                  ret = this.googledrive;
                  break;
  
              default:
                  throw new IllegalArgumentException("Unknown service!");
          }
  
          return ret.get();
      }
  
      void storePersistent() {
          SharedPreferences sharedPreferences = context.getPreferences(Context.MODE_PRIVATE);
          SharedPreferences.Editor editor = sharedPreferences.edit();
  
          editor.putString("googledrivePersistent", googledrive.get().saveAsString());
  
          editor.apply();
      }
  }
  </code></pre>
  
  <p><strong>AndroidMenifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mpathak.driverail"&gt;
  
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity
              android:name=".MainActivity"
              android:launchMode="singleTask"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
  
               &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.VIEW" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
                  &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
                  &lt;data android:scheme="com.mpathak.driverail" /&gt;
              &lt;/intent-filter&gt;
  
          &lt;/activity&gt;        
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>