Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  *
  3.  * Phonegap backup plugin for Android
  4.  * Luis Balam 2012
  5.  * @version 0.0.5
  6.  *
  7.  */
  8.  
  9. package org.opencorebanking.plugins;
  10.  
  11. import org.json.JSONArray;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15. //import android.os.*;
  16. //import android.net.*;
  17. import java.io.*;
  18. //import java.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("output");
  32.  
  33.             try {
  34.                 goBackup(pkg, dest);
  35.             } catch (FileNotFoundException e) {
  36.                 // TODO Auto-generated catch block
  37.                 e.printStackTrace();
  38.             }
  39.             return new PluginResult(PluginResult.Status.OK);
  40.         } catch (JSONException e) {
  41.             return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
  42.         }
  43.     }
  44.  
  45.     private boolean goBackup(String strPkg, String strOut) throws FileNotFoundException{
  46.         String DBPATH   = "/data/data/"  + strPkg + "/app_database/file__0/";
  47.         String DBNAME   = "0000000000000001.db";
  48.         String myPath   = DBPATH + DBNAME;
  49.         Boolean result  = false;
  50.         //Open your local db as the input stream
  51.    
  52.         InputStream myInput = new FileInputStream(myPath);
  53.    
  54.         // Path to the just created empty db
  55.         String outFileName = strOut;
  56.    
  57.         //Open the empty db as the output stream
  58.         OutputStream myOut;
  59.         try {
  60.             myOut = new FileOutputStream(outFileName);
  61.             //transfer bytes from the inputfile to the outputfile
  62.             byte[] buffer   = new byte[1024];
  63.             int length;
  64.             while ((length  = myInput.read(buffer))>0){
  65.                 myOut.write(buffer, 0, length);
  66.             }
  67.             //Close the streams
  68.             myOut.flush();
  69.             myOut.close();
  70.             myInput.close();
  71.             result = true;
  72.         } catch (FileNotFoundException e) {
  73.             // Auto-generated catch block
  74.             e.printStackTrace();
  75.             result  = false;
  76.         } catch (IOException e) {
  77.             // Auto-generated catch block
  78.             e.printStackTrace();
  79.             result  = false;
  80.         }
  81.         return result;
  82.     }
  83. }