Android Open Source - mmsmsbackup M M S M S Backup






From Project

Back to project page mmsmsbackup.

License

The source code is released under:

Apache License

If you think the Android project mmsmsbackup 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.jug6ernaut.sms;
/*from  w  w  w . j a v  a2  s  . com*/
import android.app.Activity;
import android.app.ProgressDialog;
import eu.chainfire.libsuperuser.Debug;
import eu.chainfire.libsuperuser.Shell;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;

import java.util.Arrays;
import java.util.List;

/**
 * Created by williamwebb on 3/1/14.
 */
public class MMSMSBackup {

    private static final  String TELEPHONY_FOLDER = "/data/data/com.android.providers.telephony/";

    private static final String commandCD = "cd %s";
    private static final String commandCAT = "cat %s > %s";
    private static final String commandTar = "tar -zcvf %s *";
    private static final String commandUnTar = "tar -zxvf %s";
    private static final String commandRM = "rm %s";
    private static final String commandViewArchive = "tar -tf %s %s";

    public static Observable<List<String>> restore(final Activity activity, final String savePath,final String fileName){
        final ProgressDialog dialog = launchRingDialog(activity, "restoring " + fileName + "...");
        return Observable.create(new Observable.OnSubscribe<List<String>>() {
            @Override
            public void call(final Subscriber<? super List<String>> observer) {
                final Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            getShellListener(new ShellObserver(){
                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onNext(String args) {
                                    dialog.setMessage(args);
                                }
                            });

                            observer.onNext(Shell.SU.run(Arrays.asList(
                                    String.format(commandCD, savePath),
                                    String.format(commandCAT, savePath + fileName, TELEPHONY_FOLDER + fileName),
                                    String.format(commandCD, TELEPHONY_FOLDER),
                                    String.format(commandUnTar, fileName),
                                    String.format(commandRM, fileName))));
                            observer.onCompleted();
                            dialog.dismiss();
                        } catch (Exception e) {
                            observer.onError(e);
                        }
                    }
                };
                thread.start();
            }
        }).subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread());
    }

    public static Observable<List<String>> backup(final Activity activity, final String savePath, final String fileName){
        final ProgressDialog dialog = launchRingDialog(activity, "backing up " + fileName + "...");
        return Observable.create(new Observable.OnSubscribe<List<String>>() {
            @Override
            public void call(final Subscriber<? super List<String>> observer) {
                final Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            getShellListener(new ShellObserver(){
                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onNext(String args) {
                                    dialog.setMessage(args);
                                }
                            });

                                    observer.onNext(Shell.SU.run(Arrays.asList(
                                            String.format(commandCD, TELEPHONY_FOLDER),
                                            String.format(commandTar, fileName),
                                            String.format(commandCAT, fileName, savePath + fileName),
                                            String.format(commandRM, fileName))));
                            observer.onCompleted();
                            dialog.dismiss();
                        } catch (Exception e) {
                            observer.onError(e);
                            dialog.dismiss();
                        }
                    }
                };
                thread.start();
            }
        }).subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread());
    }

    public static Observable<Boolean> validate(final String savePath, final String fileName){
        return Observable.create(new Observable.OnSubscribe<Boolean>() {
            @Override
            public void call(final Subscriber<? super Boolean> observer) {
                final Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            List<String> files = Shell.SU.run(Arrays.asList(
                                    String.format(commandViewArchive, savePath + fileName, "databases")));

                            boolean hasDBs = (
                                    files.contains("databases/mmssms.db") &&
                                            files.contains("databases/mmssms.db-journal")
                            );
                            observer.onNext(hasDBs);
                            observer.onCompleted();
                        } catch (Exception e) {
                            observer.onError(e);
                        }
                    }
                };
                thread.start();
            }
        }).subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread());
    }

    public static Observable delete(final String savePath, final String fileName){
        return Observable.create(new Observable.OnSubscribe<List<String>>() {
            @Override
            public void call(final Subscriber<? super List<String>> observer) {
                final Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            observer.onNext(Shell.SU.run(Arrays.asList(
                                    String.format(commandRM,  savePath + fileName))));
                            observer.onCompleted();
                        } catch (Exception e) {
                            observer.onError(e);
                        }
                    }
                };
                thread.start();
            }
        }).subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread());
    }

    public static Observable<Boolean> haveSU(final Activity activity){
        final ProgressDialog dialog = launchRingDialog(activity, "checking for SuperUser..");
        return Observable.create(new Observable.OnSubscribe<Boolean>() {

            @Override
            public void call(final Subscriber<? super Boolean> subscriber) {
                final Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            subscriber.onNext(Shell.SU.available());
                            subscriber.onCompleted();
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    dialog.dismiss();
                                }
                            });
                        } catch (Exception e) {
                            subscriber.onNext(false);
                            subscriber.onError(e);
                        }
                    }
                };
                thread.start();
            }
        }).subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread());
    }

    public static ProgressDialog launchRingDialog(final Activity activity,final String message) {
        final ProgressDialog ringProgressDialog = ProgressDialog.show(activity, "Please wait ...", message, true);
        ringProgressDialog.setCancelable(false);
        return ringProgressDialog;
    }

    static PublishSubject<String> loginEventPublisher = PublishSubject.create();

    public static Subscription getShellListener(Observer<String> listener){
        Subscription subscription = loginEventPublisher
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(listener);
        return subscription;

    }

    private static abstract class ShellObserver implements Observer<String> {

        protected Subscription subscription;

        @Override
        public void onCompleted() {
            subscription.unsubscribe();
        }

    }

    static {
        Debug.setLogTypeEnabled(Debug.LOG_NONE, true);
        Debug.setLogTypeEnabled(Debug.LOG_OUTPUT, true);
        Debug.setOnLogListener(new Debug.OnLogListener() {
            @Override
            public void onLog(int i, final String s, final String s2) {
                System.out.println(i + " : " + s + " : " + s2);
                loginEventPublisher.onNext(s2);
            }
        });
    }

}




Java Source Code List

com.jug6ernaut.sms.AnalysisDir.java
com.jug6ernaut.sms.EULA.java
com.jug6ernaut.sms.MMSMSBackup.java
com.jug6ernaut.sms.MainActivity.java
com.jug6ernaut.sms.MenuCreator.java