Android Open Source - box-android-sdk-v2-master Main Activity






From Project

Back to project page box-android-sdk-v2-master.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRI...

If you think the Android project box-android-sdk-v2-master listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.helloworld;
/*from   w ww. j a v  a  2  s .c om*/
import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ObjectUtils.Null;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.box.boxandroidlibv2.BoxAndroidClient;
import com.box.boxandroidlibv2.activities.FilePickerActivity;
import com.box.boxandroidlibv2.activities.FolderPickerActivity;
import com.box.boxandroidlibv2.activities.OAuthActivity;
import com.box.boxandroidlibv2.dao.BoxAndroidFile;
import com.box.boxandroidlibv2.dao.BoxAndroidFolder;
import com.box.boxandroidlibv2.dao.BoxAndroidOAuthData;
import com.box.boxjavalibv2.exceptions.AuthFatalFailureException;
import com.box.boxjavalibv2.requests.requestobjects.BoxFileUploadRequestObject;

public class MainActivity extends Activity {

    private final static int AUTH_REQUEST = 1;
    private final static int UPLOAD_REQUEST = 2;
    private final static int DOWNLOAD_REQUEST = 3;

    private Button btnUpload;
    private Button btnAuth;
    private Button btnDownload;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initUI();
    }

    private void initUI() {
        initAuthButton();
        initUploadButton();
        initDownloadButton();
    }

    private void initAuthButton() {
        btnAuth = (Button) findViewById(R.id.authenticate);
        btnAuth.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startAuthentication();
            }

        });
    }

    private void startAuthentication() {
        Intent intent = OAuthActivity.createOAuthActivityIntent(this, HelloWorldApplication.CLIENT_ID, HelloWorldApplication.CLIENT_SECRET, false,
            HelloWorldApplication.REDIRECT_URL);
        this.startActivityForResult(intent, AUTH_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AUTH_REQUEST) {
            onAuthenticated(resultCode, data);
        }
        else if (requestCode == UPLOAD_REQUEST) {
            onFolderSelected(resultCode, data);
        }
        else if (requestCode == DOWNLOAD_REQUEST) {
            onFileSelected(resultCode, data);
        }
    }

    private void onAuthenticated(int resultCode, Intent data) {
        if (Activity.RESULT_OK != resultCode) {
            Toast.makeText(this, "fail", Toast.LENGTH_LONG).show();
        }
        else {
            BoxAndroidOAuthData oauth = data.getParcelableExtra(OAuthActivity.BOX_CLIENT_OAUTH);
            BoxAndroidClient client = new BoxAndroidClient(HelloWorldApplication.CLIENT_ID, HelloWorldApplication.CLIENT_SECRET, null, null);
            client.authenticate(oauth);
            if (client == null) {
                Toast.makeText(this, "fail", Toast.LENGTH_LONG).show();
            }
            else {
                ((HelloWorldApplication) getApplication()).setClient(client);
                Toast.makeText(this, "authenticated", Toast.LENGTH_LONG).show();
            }
        }
    }

    private void initDownloadButton() {
        btnDownload = (Button) findViewById(R.id.downloadfile);
        btnDownload.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                doDownload();
            }

        });
    }

    private void initUploadButton() {
        btnUpload = (Button) findViewById(R.id.uploadfile);
        btnUpload.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                doUpload();
            }

        });
    }

    private void onFileSelected(int resultCode, Intent data) {
        if (Activity.RESULT_OK != resultCode) {
            Toast.makeText(this, "fail", Toast.LENGTH_LONG).show();
        }
        else {
            final BoxAndroidFile file = data.getParcelableExtra(FilePickerActivity.EXTRA_BOX_ANDROID_FILE);
            AsyncTask<Null, Integer, Null> task = new AsyncTask<Null, Integer, Null>() {

                @Override
                protected void onPostExecute(Null result) {
                    Toast.makeText(MainActivity.this, "done downloading", Toast.LENGTH_LONG).show();
                    super.onPostExecute(result);
                }

                @Override
                protected void onPreExecute() {
                    Toast.makeText(MainActivity.this, "start downloading", Toast.LENGTH_LONG).show();
                    super.onPreExecute();
                }

                @Override
                protected Null doInBackground(Null... params) {
                    BoxAndroidClient client = ((HelloWorldApplication) getApplication()).getClient();
                    try {
                        File f = new File(Environment.getExternalStorageDirectory(), file.getName());
                        System.out.println(f.getAbsolutePath());
                        client.getFilesManager().downloadFile(file.getId(), f, null, null);
                    }
                    catch (Exception e) {
                    }
                    return null;
                }
            };
            task.execute();

        }
    }

    private void onFolderSelected(int resultCode, Intent data) {
        if (Activity.RESULT_OK != resultCode) {
            Toast.makeText(this, "fail", Toast.LENGTH_LONG).show();
        }
        else {
            final BoxAndroidFolder folder = data.getParcelableExtra(FolderPickerActivity.EXTRA_BOX_ANDROID_FOLDER);
            AsyncTask<Null, Integer, Null> task = new AsyncTask<Null, Integer, Null>() {

                @Override
                protected void onPostExecute(Null result) {
                    Toast.makeText(MainActivity.this, "done uploading", Toast.LENGTH_LONG).show();
                    super.onPostExecute(result);
                }

                @Override
                protected void onPreExecute() {
                    Toast.makeText(MainActivity.this, "start uploading", Toast.LENGTH_LONG).show();
                    super.onPreExecute();
                }

                @Override
                protected Null doInBackground(Null... params) {
                    BoxAndroidClient client = ((HelloWorldApplication) getApplication()).getClient();
                    try {
                        File mockFile = createMockFile();
                        client.getFilesManager().uploadFile(
                            BoxFileUploadRequestObject.uploadFileRequestObject(folder.getId(), mockFile.getName(), mockFile, client.getJSONParser()));
                    }
                    catch (Exception e) {
                    }
                    return null;
                }
            };
            task.execute();
        }
    }

    private void doUpload() {
        try {
            BoxAndroidClient client = ((HelloWorldApplication) getApplication()).getClient();
            Intent intent = FolderPickerActivity.getLaunchIntent(this, "0", (BoxAndroidOAuthData) client.getAuthData(), HelloWorldApplication.CLIENT_ID,
                HelloWorldApplication.CLIENT_SECRET);
            startActivityForResult(intent, UPLOAD_REQUEST);
        }
        catch (AuthFatalFailureException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void doDownload() {
        try {
            BoxAndroidClient client = ((HelloWorldApplication) getApplication()).getClient();
            Intent intent = FilePickerActivity.getLaunchIntent(this, "0", (BoxAndroidOAuthData) client.getAuthData(), HelloWorldApplication.CLIENT_ID,
                HelloWorldApplication.CLIENT_SECRET);
            startActivityForResult(intent, DOWNLOAD_REQUEST);
        }
        catch (AuthFatalFailureException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private File createMockFile() {
        try {
            File file = File.createTempFile("tmp", ".txt");
            FileUtils.writeStringToFile(file, "string");
            return file;
        }
        catch (Exception e) {
            return null;
        }
    }
}




Java Source Code List

com.box.android.sample.BoxSDKSampleApplication.java
com.box.android.sample.FileListActivity.java
com.box.android.sample.FileListAdapter.java
com.box.androidlibv2.sample.oauth.OAuthActivity.java
com.box.androidlibv2.sample.oauth.OAuthSampleApplication.java
com.box.boxandroidlibv2.BoxAndroidClient.java
com.box.boxandroidlibv2.BoxAndroidConfig.java
com.box.boxandroidlibv2.activities.FilePickerActivity.java
com.box.boxandroidlibv2.activities.FolderNavigationActivity.java
com.box.boxandroidlibv2.activities.FolderPickerActivity.java
com.box.boxandroidlibv2.activities.OAuthActivity.java
com.box.boxandroidlibv2.adapters.BoxListItemAdapter.java
com.box.boxandroidlibv2.adapters.NavigationListAdapter.java
com.box.boxandroidlibv2.dao.BoxAndroidCollaboration.java
com.box.boxandroidlibv2.dao.BoxAndroidCollection.java
com.box.boxandroidlibv2.dao.BoxAndroidComment.java
com.box.boxandroidlibv2.dao.BoxAndroidEmailAlias.java
com.box.boxandroidlibv2.dao.BoxAndroidEmail.java
com.box.boxandroidlibv2.dao.BoxAndroidEnterprise.java
com.box.boxandroidlibv2.dao.BoxAndroidEventCollection.java
com.box.boxandroidlibv2.dao.BoxAndroidEvent.java
com.box.boxandroidlibv2.dao.BoxAndroidFileVersion.java
com.box.boxandroidlibv2.dao.BoxAndroidFile.java
com.box.boxandroidlibv2.dao.BoxAndroidFolder.java
com.box.boxandroidlibv2.dao.BoxAndroidGroupMembership.java
com.box.boxandroidlibv2.dao.BoxAndroidGroup.java
com.box.boxandroidlibv2.dao.BoxAndroidOAuthData.java
com.box.boxandroidlibv2.dao.BoxAndroidSharedLinkPermissions.java
com.box.boxandroidlibv2.dao.BoxAndroidSharedLink.java
com.box.boxandroidlibv2.dao.BoxAndroidUser.java
com.box.boxandroidlibv2.dao.BoxAndroidWebLink.java
com.box.boxandroidlibv2.dao.BoxParcel.java
com.box.boxandroidlibv2.exceptions.BoxAndroidLibException.java
com.box.boxandroidlibv2.exceptions.IBoxAndroidLibException.java
com.box.boxandroidlibv2.exceptions.UserTerminationException.java
com.box.boxandroidlibv2.jsonparsing.AndroidBoxResourceHub.java
com.box.boxandroidlibv2.manager.ThumbnailManager.java
com.box.boxandroidlibv2.viewdata.BoxListItem.java
com.box.boxandroidlibv2.viewdata.NavigationItem.java
com.box.boxandroidlibv2.viewlisteners.OAuthWebViewListener.java
com.box.boxandroidlibv2.viewlisteners.StringMessage.java
com.box.boxandroidlibv2.views.OAuthWebView.java
com.example.helloworld.HelloWorldApplication.java
com.example.helloworld.MainActivity.java