1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.messaging.datamodel.action; 18 19 import android.content.Context; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 23 import com.android.messaging.Factory; 24 import com.android.messaging.datamodel.DatabaseHelper; 25 import com.android.messaging.util.DebugUtils; 26 import com.android.messaging.util.LogUtil; 27 28 import java.io.BufferedInputStream; 29 import java.io.BufferedOutputStream; 30 import java.io.File; 31 import java.io.FileInputStream; 32 import java.io.FileOutputStream; 33 import java.io.IOException; 34 35 public class DumpDatabaseAction extends Action implements Parcelable { 36 private static final String TAG = LogUtil.BUGLE_DATAMODEL_TAG; 37 public static final String DUMP_NAME = "db_copy.db"; 38 private static final int BUFFER_SIZE = 16384; 39 40 /** 41 * Copy the database to external storage 42 */ dumpDatabase()43 public static void dumpDatabase() { 44 final DumpDatabaseAction action = new DumpDatabaseAction(); 45 action.start(); 46 } 47 DumpDatabaseAction()48 private DumpDatabaseAction() { 49 } 50 51 @Override executeAction()52 protected Object executeAction() { 53 final Context context = Factory.get().getApplicationContext(); 54 final String dbName = DatabaseHelper.DATABASE_NAME; 55 BufferedOutputStream bos = null; 56 BufferedInputStream bis = null; 57 58 long originalSize = 0; 59 final File inFile = context.getDatabasePath(dbName); 60 if (inFile.exists() && inFile.isFile()) { 61 originalSize = inFile.length(); 62 } 63 final File outFile = DebugUtils.getDebugFile(DUMP_NAME, true); 64 if (outFile != null) { 65 int totalBytes = 0; 66 try { 67 bos = new BufferedOutputStream(new FileOutputStream(outFile)); 68 bis = new BufferedInputStream(new FileInputStream(inFile)); 69 70 final byte[] buffer = new byte[BUFFER_SIZE]; 71 int bytesRead; 72 while ((bytesRead = bis.read(buffer)) > 0) { 73 bos.write(buffer, 0, bytesRead); 74 totalBytes += bytesRead; 75 } 76 } catch (final IOException e) { 77 LogUtil.w(TAG, "Exception copying the database;" 78 + " destination may not be complete.", e); 79 } finally { 80 if (bos != null) { 81 try { 82 bos.close(); 83 } catch (final IOException e) { 84 // Nothing to do 85 } 86 } 87 88 if (bis != null) { 89 try { 90 bis.close(); 91 } catch (final IOException e) { 92 // Nothing to do 93 } 94 } 95 DebugUtils.ensureReadable(outFile); 96 LogUtil.i(TAG, "Dump complete; orig size: " + originalSize + 97 ", copy size: " + totalBytes); 98 } 99 } 100 return null; 101 } 102 DumpDatabaseAction(final Parcel in)103 private DumpDatabaseAction(final Parcel in) { 104 super(in); 105 } 106 107 public static final Parcelable.Creator<DumpDatabaseAction> CREATOR 108 = new Parcelable.Creator<DumpDatabaseAction>() { 109 @Override 110 public DumpDatabaseAction createFromParcel(final Parcel in) { 111 return new DumpDatabaseAction(in); 112 } 113 114 @Override 115 public DumpDatabaseAction[] newArray(final int size) { 116 return new DumpDatabaseAction[size]; 117 } 118 }; 119 120 @Override writeToParcel(final Parcel parcel, final int flags)121 public void writeToParcel(final Parcel parcel, final int flags) { 122 writeActionToParcel(parcel, flags); 123 } 124 } 125