EMMA Coverage Report (generated Fri Mar 10 17:09:17 CET 2017)
[all classes][kdk.android.simplydo]

COVERAGE SUMMARY FOR SOURCE FILE [RestoreActivity.java]

nameclass, %method, %block, %line, %
RestoreActivity.java100% (6/6)94%  (16/17)83%  (265/319)80%  (63/79)

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 RestoreActivity100% (1/1)100% (7/7)81%  (209/257)77%  (50/65)
doRestore (): void 100% (1/1)61%  (49/80)62%  (16/26)
onListItemClick (ListView, View, int, long): void 100% (1/1)71%  (32/45)67%  (8/12)
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% (8/8)
refresh (): void 100% (1/1)100% (49/49)100% (9/9)
     
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$3100% (1/1)100% (2/2)100% (9/9)100% (3/3)
RestoreActivity$3 (RestoreActivity): void 100% (1/1)100% (6/6)100% (1/1)
onClick (DialogInterface, int): void 100% (1/1)100% (3/3)100% (2/2)
     
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                   public void onClick(DialogInterface dialog, int id) {
88                       doRestore();
89                       dialog.cancel();
90                   }
91               })
92               .setNegativeButton(R.string.restoreWarnNegative, new DialogInterface.OnClickListener() {
93                   public void onClick(DialogInterface dialog, int id) {
94                        dialog.cancel();
95                   }
96               });
97 
98    }
99    
100    
101    @Override
102    protected Dialog onCreateDialog(int id)
103    {
104        Log.v(L.TAG, "RestoreActivity.onCreateDialog() called");
105                
106        switch(id)
107        {
108            case DIALOG_RESTORE_WARN:
109            {
110                AlertDialog dialog = restoreWarningBuilder.create();
111                return dialog;
112            }
113        }
114        
115        return super.onCreateDialog(id);
116    }
117    
118    @Override
119    protected void onListItemClick(ListView l, View v, int position, long id)
120    {
121        super.onListItemClick(l, v, position, id);
122        
123        Log.i(L.TAG, "RestoreActivity.onListItemClick()");
124 
125        restoreFile = adapter.getItem(position);
126        
127        try
128        {
129            // test restore file
130            SQLiteDatabase db = SQLiteDatabase.openDatabase(
131                    restoreFile.file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
132            db.close();
133            
134            // Dialog: This will overwrite the existing items, continue?
135            showDialog(DIALOG_RESTORE_WARN);
136        }
137        catch(Exception e)
138        {
139            Log.e(L.TAG, "Error testing user selected restore DB", e);
140            Toast t = Toast.makeText(this, R.string.restoreToastInvalidDB, Toast.LENGTH_LONG);
141            t.show();
142        }
143        
144    }
145 
146    
147    private void refresh()
148    {
149        File backupDirectory = new File(
150                Environment.getExternalStorageDirectory(), 
151                "/Android/data/kdk.android.simplydo/files/");
152        adapter.clear();
153        if(backupDirectory.isDirectory())
154        {
155            File[] files = backupDirectory.listFiles(restoreFilenameFilter);
156            for(File f : files)
157            {
158                adapter.add(new NameOnlyFile(f));
159            }
160            adapter.sort(comparator);
161        }
162        adapter.notifyDataSetChanged();
163    }
164    
165    
166    private void doRestore()
167    {
168        Log.i(L.TAG, "RestoreActivity.doRestore() called");
169        
170        // Flush the database update queue        
171        SimplyDoActivity.getInstance().getDataVeiwer().flush();
172        
173        String state = Environment.getExternalStorageState();
174        if (!Environment.MEDIA_MOUNTED.equals(state)) 
175        {
176            Toast.makeText(
177                    this, 
178                    R.string.restoreToastMountProblem, 
179                    Toast.LENGTH_LONG
180                    ).show();
181            return;
182        }        
183        
184        // backup old file
185        File dbFile = getDatabasePath(DataManager.DATABASE_NAME);
186        File dbBakFile = getDatabasePath(DataManager.DATABASE_NAME + ".bak");
187        boolean moved = dbFile.renameTo(dbBakFile);
188        if(!moved)
189        {
190            Toast.makeText(
191                    this, 
192                    R.string.restoreToastUnableToMove, 
193                    Toast.LENGTH_LONG
194                    ).show();
195            return;
196        }
197 
198        try
199        {
200            // copy new file into place
201            SettingsActivity.fileCopy(restoreFile.file, dbFile);
202            
203            // delete backup
204            dbBakFile.delete();
205        }
206        catch (Exception e)
207        {
208            // put the old database back
209            dbFile.delete();
210            dbBakFile.renameTo(dbFile);
211            
212            Log.e(L.TAG,  "Failed to copy restore database into place", e);
213            
214            Toast.makeText(
215                    this, 
216                    R.string.restoreToastCopyFailed, 
217                    Toast.LENGTH_LONG
218                    ).show();
219            return;
220        }
221        
222        SimplyDoActivity.getInstance().getDataVeiwer().invalidateCache();
223        SimplyDoActivity.getInstance().cacheInvalidated();
224        
225        Toast.makeText(
226                this, 
227                R.string.restoreToastRestoreFinished, 
228                Toast.LENGTH_LONG
229                ).show();
230        
231        finish();
232    }
233 
234    
235    private static class NameOnlyFile
236    {
237        public File file;
238        
239        public NameOnlyFile(File f)
240        {
241            file = f;
242        }
243 
244        @Override
245        public String toString()
246        {
247            String name = file.getName();
248            return name.substring(0, name.length() - EXTENSION.length());
249        }
250    }
251}

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