EMMA Coverage Report (generated Fri Jan 29 20:09:05 CET 2016)
[all classes][kdk.android.simplydo]

COVERAGE SUMMARY FOR SOURCE FILE [RestoreActivity.java]

nameclass, %method, %block, %line, %
RestoreActivity.java100% (6/6)88%  (15/17)82%  (262/319)76%  (66/87)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class RestoreActivity$2100% (1/1)50%  (1/2)50%  (6/12)50%  (1/2)
compare (RestoreActivity$NameOnlyFile, RestoreActivity$NameOnlyFile): int 0%   (0/1)0%   (0/6)0%   (0/1)
RestoreActivity$2 (RestoreActivity): void 100% (1/1)100% (6/6)100% (1/1)
     
class RestoreActivity$3100% (1/1)50%  (1/2)67%  (6/9)33%  (1/3)
onClick (DialogInterface, int): void 0%   (0/1)0%   (0/3)0%   (0/2)
RestoreActivity$3 (RestoreActivity): void 100% (1/1)100% (6/6)100% (1/1)
     
class RestoreActivity100% (1/1)100% (7/7)81%  (209/257)76%  (57/75)
doRestore (): void 100% (1/1)61%  (49/80)57%  (17/30)
onListItemClick (ListView, View, int, long): void 100% (1/1)71%  (32/45)69%  (9/13)
onCreateDialog (int): Dialog 100% (1/1)75%  (12/16)80%  (4/5)
RestoreActivity (): void 100% (1/1)100% (15/15)100% (4/4)
access$000 (RestoreActivity): void 100% (1/1)100% (3/3)100% (1/1)
onCreate (Bundle): void 100% (1/1)100% (49/49)100% (12/12)
refresh (): void 100% (1/1)100% (49/49)100% (10/10)
     
class RestoreActivity$1100% (1/1)100% (2/2)100% (10/10)100% (2/2)
RestoreActivity$1 (RestoreActivity): void 100% (1/1)100% (6/6)100% (1/1)
accept (File, String): boolean 100% (1/1)100% (4/4)100% (1/1)
     
class RestoreActivity$4100% (1/1)100% (2/2)100% (12/12)100% (4/4)
RestoreActivity$4 (RestoreActivity): void 100% (1/1)100% (6/6)100% (1/1)
onClick (DialogInterface, int): void 100% (1/1)100% (6/6)100% (3/3)
     
class RestoreActivity$NameOnlyFile100% (1/1)100% (2/2)100% (19/19)100% (5/5)
RestoreActivity$NameOnlyFile (File): void 100% (1/1)100% (6/6)100% (3/3)
toString (): String 100% (1/1)100% (13/13)100% (2/2)

1/*
2 * Copyright (C) 2013 Keith Kildare
3 * 
4 * This file is part of SimplyDo.
5 * 
6 * SimplyDo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * 
11 * SimplyDo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 * 
16 * You should have received a copy of the GNU General Public License
17 * along with SimplyDo.  If not, see <http://www.gnu.org/licenses/>.
18 * 
19 */
20package kdk.android.simplydo;
21 
22import java.io.File;
23import java.io.FilenameFilter;
24import java.util.Comparator;
25 
26import android.app.AlertDialog;
27import android.app.Dialog;
28import android.app.ListActivity;
29import android.content.DialogInterface;
30import android.database.sqlite.SQLiteDatabase;
31import android.os.Bundle;
32import android.os.Environment;
33import android.util.Log;
34import android.view.View;
35import android.widget.ArrayAdapter;
36import android.widget.ListView;
37import android.widget.Toast;
38 
39public class RestoreActivity extends ListActivity 
40{
41    private static final int DIALOG_RESTORE_WARN = 300;
42    private static final String EXTENSION = ".simplydo";
43 
44    private ArrayAdapter<NameOnlyFile> adapter;
45    private AlertDialog.Builder restoreWarningBuilder;
46    private NameOnlyFile restoreFile;
47    private FilenameFilter restoreFilenameFilter;
48    private Comparator<NameOnlyFile> comparator;
49    
50    public RestoreActivity()
51    {
52        restoreFilenameFilter = new FilenameFilter() {
53            @Override
54            public boolean accept(File dir, String filename)
55            {
56                return filename.endsWith(EXTENSION);
57            }
58        };
59        
60        comparator = new Comparator<NameOnlyFile>() {
61            @Override
62            public int compare(NameOnlyFile object1, NameOnlyFile object2)
63            {
64                return object2.toString().compareTo(object1.toString());
65            }
66        };
67    }
68 
69    @Override
70    protected void onCreate(Bundle savedInstanceState)
71    {
72        super.onCreate(savedInstanceState);
73        
74        Log.v(L.TAG, "RestoreActivity.onCreate() called");
75 
76        adapter = new ArrayAdapter<NameOnlyFile>(this, R.layout.restore_entry, R.id.RestoreName);
77        
78        refresh();
79        
80        setListAdapter(adapter);
81        
82        restoreWarningBuilder = new AlertDialog.Builder(this);
83        restoreWarningBuilder.setMessage(R.string.restoreWarnMessage)
84               .setCancelable(true)
85               .setTitle(R.string.restoreWarnTitle)
86               .setPositiveButton(R.string.restoreWarnPositive, new DialogInterface.OnClickListener() {
87                   @Override
88                                public void onClick(DialogInterface dialog, int id) {
89                       doRestore();
90                       dialog.cancel();
91                   }
92               })
93               .setNegativeButton(R.string.restoreWarnNegative, new DialogInterface.OnClickListener() {
94                   @Override
95                                public void onClick(DialogInterface dialog, int id) {
96                        dialog.cancel();
97                   }
98               });
99 
100    }
101    
102    
103    @Override
104    protected Dialog onCreateDialog(int id)
105    {
106        Log.v(L.TAG, "RestoreActivity.onCreateDialog() called");
107                
108        switch(id)
109        {
110            case DIALOG_RESTORE_WARN:
111            {
112                AlertDialog dialog = restoreWarningBuilder.create();
113                return dialog;
114            }
115        }
116        
117        return super.onCreateDialog(id);
118    }
119    
120    @Override
121    protected void onListItemClick(ListView l, View v, int position, long id)
122    {
123        super.onListItemClick(l, v, position, id);
124        
125        Log.i(L.TAG, "RestoreActivity.onListItemClick()");
126 
127        restoreFile = adapter.getItem(position);
128        
129        try
130        {
131            // test restore file
132            SQLiteDatabase db = SQLiteDatabase.openDatabase(
133                    restoreFile.file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
134            db.close();
135            
136            // Dialog: This will overwrite the existing items, continue?
137            showDialog(DIALOG_RESTORE_WARN);
138        }
139        catch(Exception e)
140        {
141            Log.e(L.TAG, "Error testing user selected restore DB", e);
142            Toast t = Toast.makeText(this, R.string.restoreToastInvalidDB, Toast.LENGTH_LONG);
143            t.show();
144        }
145        
146    }
147 
148    
149    private void refresh()
150    {
151        File backupDirectory = new File(
152                Environment.getExternalStorageDirectory(), 
153                "/Android/data/kdk.android.simplydo/files/");
154        adapter.clear();
155        if(backupDirectory.isDirectory())
156        {
157            File[] files = backupDirectory.listFiles(restoreFilenameFilter);
158            for(File f : files)
159            {
160                adapter.add(new NameOnlyFile(f));
161            }
162            adapter.sort(comparator);
163        }
164        adapter.notifyDataSetChanged();
165    }
166    
167    
168    private void doRestore()
169    {
170        Log.i(L.TAG, "RestoreActivity.doRestore() called");
171        
172        // Flush the database update queue        
173        SimplyDoActivity.getInstance().getDataVeiwer().flush();
174        
175        String state = Environment.getExternalStorageState();
176        if (!Environment.MEDIA_MOUNTED.equals(state)) 
177        {
178            Toast.makeText(
179                    this, 
180                    R.string.restoreToastMountProblem, 
181                    Toast.LENGTH_LONG
182                    ).show();
183            return;
184        }        
185        
186        // backup old file
187        File dbFile = getDatabasePath(DataManager.DATABASE_NAME);
188        File dbBakFile = getDatabasePath(DataManager.DATABASE_NAME + ".bak");
189        boolean moved = dbFile.renameTo(dbBakFile);
190        if(!moved)
191        {
192            Toast.makeText(
193                    this, 
194                    R.string.restoreToastUnableToMove, 
195                    Toast.LENGTH_LONG
196                    ).show();
197            return;
198        }
199 
200        try
201        {
202            // copy new file into place
203            SettingsActivity.fileCopy(restoreFile.file, dbFile);
204            
205            // delete backup
206            dbBakFile.delete();
207        }
208        catch (Exception e)
209        {
210            // put the old database back
211            dbFile.delete();
212            dbBakFile.renameTo(dbFile);
213            
214            Log.e(L.TAG,  "Failed to copy restore database into place", e);
215            
216            Toast.makeText(
217                    this, 
218                    R.string.restoreToastCopyFailed, 
219                    Toast.LENGTH_LONG
220                    ).show();
221            return;
222        }
223        
224        SimplyDoActivity.getInstance().getDataVeiwer().invalidateCache();
225        SimplyDoActivity.getInstance().cacheInvalidated();
226        
227        Toast.makeText(
228                this, 
229                R.string.restoreToastRestoreFinished, 
230                Toast.LENGTH_LONG
231                ).show();
232        
233        finish();
234    }
235 
236    
237    private static class NameOnlyFile
238    {
239        public File file;
240        
241        public NameOnlyFile(File f)
242        {
243            file = f;
244        }
245 
246        @Override
247        public String toString()
248        {
249            String name = file.getName();
250            return name.substring(0, name.length() - EXTENSION.length());
251        }
252    }
253}

[all classes][kdk.android.simplydo]
EMMA 2.0.5312 (C) Vladimir Roubtsov