Android Open Source - SmsToDropbox Main Activity






From Project

Back to project page SmsToDropbox.

License

The source code is released under:

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

If you think the Android project SmsToDropbox 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.unifyx.smstodropbox;
//  w  w  w . ja v  a  2 s.c o m
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.dropbox.sync.android.DbxAccountManager;
import com.dropbox.sync.android.DbxFile;
import com.dropbox.sync.android.DbxFileSystem;
import com.dropbox.sync.android.DbxPath;
import com.tuenti.smsradar.Sms;
import com.tuenti.smsradar.SmsListener;
import com.tuenti.smsradar.SmsRadar;

import java.io.IOException;

public class MainActivity extends Activity {

    public static final String TAG = "com.unifyx.smstodropbox";
    static final int REQUEST_LINK_TO_DBX = 1010;  // This value is up to you
    private static final String APP_KEY = "9fx5p1zwobullg5";
    private static final String APP_SECRET = "5zbzlfpyeuotyrq";
    Switch dropboxSwitch;
    Switch smsSwitch;
    Switch toastSwitch;
    private DbxAccountManager mDbxAcctMgr;

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

        mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), APP_KEY, APP_SECRET);
        dropboxSwitch = (Switch) findViewById(R.id.dropboxSwitch);

        dropboxSwitch.setChecked(mDbxAcctMgr.hasLinkedAccount());
        dropboxSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (dropboxSwitch.isChecked()) {
                    smsSwitch.setChecked(true);
                    toastSwitch.setChecked(true);
                    showSmsToast("dropboxInit()");
                    dropboxInit();
                } else {
                    smsSwitch.setChecked(false);
                    toastSwitch.setChecked(false);
                    showSmsToast("dropboxStop()");
                    dropboxStop();
                }
            }
        });

        smsSwitch = (Switch) findViewById(R.id.smsSwitch);
        smsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (dropboxSwitch.isChecked()) {
                    if (smsSwitch.isChecked()) {
                        toastSwitch.setChecked(true);
                        showSmsToast("smsInit()");
                        smsInit();
                    } else {
                        toastSwitch.setChecked(false);
                        showSmsToast("smsStop()");
                        smsStop();
                    }
                } else {
                    smsSwitch.setChecked(false);
                }
            }
        });

        toastSwitch = (Switch) findViewById(R.id.toastSwitch);
        toastSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!smsSwitch.isChecked()) {
                    toastSwitch.setChecked(false);
                }
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "resultCode = " + resultCode);
        if (requestCode == REQUEST_LINK_TO_DBX) {
            if (resultCode == Activity.RESULT_OK) {
                if (smsSwitch.isChecked()) {
                    showSmsToast("SMS received will now be uploaded to dropbox");
                }
            } else {
                if (smsSwitch.isChecked()) {
                    showSmsToast("Cannot link account.");
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void smsInit() {
        SmsRadar.initializeSmsRadarService(this, new SmsListener() {
            @Override
            public void onSmsSent(Sms sms) {
                if (toastSwitch.isChecked()) {
                    showSmsToast(sms.toString());
                }
            }

            @Override
            public void onSmsReceived(Sms sms) {
                if (toastSwitch.isChecked()) {
                    showSmsToast(sms.toString());
                }
                dropboxUpload(sms.getDate() + ".txt", sms.toString());
            }
        });
    }

    public void smsStop() {
        SmsRadar.stopSmsRadarService(this);
    }

    public void showSmsToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    public void dropboxInit() {
        if (!mDbxAcctMgr.hasLinkedAccount()) {
            mDbxAcctMgr.startLink(this, REQUEST_LINK_TO_DBX);
        }
    }

    public void dropboxStop() {
        if (mDbxAcctMgr.hasLinkedAccount()) {
            mDbxAcctMgr.unlink();
        }
    }

    public void dropboxUpload(String filename, String message) {
        DbxFile testFile = null;
        try {
            Log.d(TAG, filename + " uploaded.");
            testFile = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount()).create(new DbxPath(filename));
            testFile.writeString(message);
            testFile.close();
        } catch (IOException e) {
            Log.d(TAG, "Error on upload.");
        }
    }
}




Java Source Code List

com.tuenti.smsradar.SharedPreferencesSmsStorage.java
com.tuenti.smsradar.SmsCursorParser.java
com.tuenti.smsradar.SmsListener.java
com.tuenti.smsradar.SmsObserver.java
com.tuenti.smsradar.SmsRadarService.java
com.tuenti.smsradar.SmsRadar.java
com.tuenti.smsradar.SmsStorage.java
com.tuenti.smsradar.SmsType.java
com.tuenti.smsradar.Sms.java
com.tuenti.smsradar.TimeProvider.java
com.unifyx.smstodropbox.ApplicationTest.java
com.unifyx.smstodropbox.MainActivity.java