Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Phonegap backup plugin for Android
  3.  * Luis Balam 2012
  4.  * @version 0.0.9
  5.  *
  6.  */
  7.  
  8. package org.opencorebanking.plugins;
  9.  
  10. import org.json.JSONArray;
  11. import org.json.JSONException;
  12. import org.json.JSONObject;
  13.  
  14. //import android.os.*;
  15. //import android.net.*;
  16. import java.io.*;
  17. //import java.util.*;
  18. import android.util.*;
  19.  
  20. import org.apache.cordova.api.Plugin;
  21. import org.apache.cordova.api.PluginResult;
  22.  
  23. public class LocalBackup extends Plugin {
  24.  
  25.    
  26.     @Override
  27.     public PluginResult execute(String action, JSONArray args, String callbackId) {
  28.         try {
  29.             JSONObject jo   = args.getJSONObject(0);
  30.             String pkg      = jo.getString("packageName");
  31.             String dest     = jo.getString("backupName");
  32.             //Log.e("Web Console", action);
  33.             try {
  34.                 if( action.equals("backup")){
  35.                     goBackup(pkg, dest);
  36.                 } else {
  37.                     goRestore(pkg, dest);
  38.                 }
  39.             } catch (FileNotFoundException e) {
  40.                 // TODO Auto-generated catch block
  41.                 e.printStackTrace();
  42.             }
  43.             return new PluginResult(PluginResult.Status.OK);
  44.         } catch (JSONException e) {
  45.             return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
  46.         }
  47.     }
  48.  
  49.     private boolean goBackup(String strPkg, String strOut) throws FileNotFoundException{
  50.         String DBPATH   = "/data/data/"  + strPkg + "/app_database/file__0/";
  51.         String DBNAME   = "0000000000000001.db";
  52.         String myPath   = DBPATH + DBNAME;
  53.         Boolean result  = false;
  54.         //Open your local db as the input stream
  55.    
  56.         InputStream myInput = new FileInputStream(myPath);
  57.    
  58.         // Path to the just created empty db
  59.         String outFileName  = strOut;
  60.    
  61.         //Open the empty db as the output stream
  62.         OutputStream myOut;
  63.         try {
  64.             myOut = new FileOutputStream(outFileName);
  65.             //transfer bytes from the inputfile to the outputfile
  66.             byte[] buffer   = new byte[1024];
  67.             int length;
  68.             while ((length  = myInput.read(buffer))>0){
  69.                 myOut.write(buffer, 0, length);
  70.             }
  71.             //Close the streams
  72.             myOut.flush();
  73.             myOut.close();
  74.             myInput.close();
  75.             result = true;
  76.         } catch (FileNotFoundException e) {
  77.             // Auto-generated catch block
  78.             e.printStackTrace();
  79.             result  = false;
  80.         } catch (IOException e) {
  81.             // Auto-generated catch block
  82.             e.printStackTrace();
  83.             result  = false;
  84.         }
  85.         return result;
  86.     }
  87.     private boolean goRestore(String strPkg, String strIn) throws FileNotFoundException{
  88.         String DBPATH   = "/data/data/"  + strPkg + "/app_database/file__0/";
  89.         String DBNAME   = "0000000000000001.db";
  90.         String myPath   = DBPATH + DBNAME;
  91.         Boolean result  = false;
  92.         //Open your local db as the input stream
  93.        
  94.         InputStream myInput = new FileInputStream(strIn); //myPath);
  95.    
  96.         // Path to the just created empty db
  97.         String outFileName  = myPath; //strIn;
  98.    
  99.         //Open the empty db as the output stream
  100.         OutputStream myOut;
  101.         try {
  102.             myOut = new FileOutputStream(outFileName);
  103.             //transfer bytes from the inputfile to the outputfile
  104.             byte[] buffer   = new byte[1024];
  105.             int length;
  106.             while ((length  = myInput.read(buffer))>0){
  107.                 myOut.write(buffer, 0, length);
  108.             }
  109.             //Close the streams
  110.             myOut.flush();
  111.             myOut.close();
  112.             myInput.close();
  113.             result = true;
  114.         } catch (FileNotFoundException e) {
  115.             // Auto-generated catch block
  116.             e.printStackTrace();
  117.             result  = false;
  118.         } catch (IOException e) {
  119.             // Auto-generated catch block
  120.             e.printStackTrace();
  121.             result  = false;
  122.         }
  123.         return result;
  124.     }
  125. }