mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 09:38:52 -06:00
Remove use of static context from HabitsApplication class (#224)
Remove static context from application class. Replace static method to obtain context with appropriate dependency. Remove context from controller. Add auto factory to export db task.
This commit is contained in:
@@ -225,7 +225,7 @@ public class BaseViewTest extends BaseAndroidTest
|
||||
throws IOException
|
||||
{
|
||||
File dir = FileUtils.getSDCardDir("test-screenshots");
|
||||
if (dir == null) dir = FileUtils.getFilesDir("test-screenshots");
|
||||
if (dir == null) dir = FileUtils.getFilesDir(testContext,"test-screenshots");
|
||||
if (dir == null) throw new RuntimeException(
|
||||
"Could not find suitable dir for screenshots");
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ public class ImportTest extends BaseAndroidTest
|
||||
|
||||
fixtures.purgeHabits(habitList);
|
||||
context = InstrumentationRegistry.getInstrumentation().getContext();
|
||||
baseDir = FileUtils.getFilesDir("Backups");
|
||||
baseDir = FileUtils.getFilesDir(context, "Backups");
|
||||
if (baseDir == null) fail("baseDir should not be null");
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ExportCSVTaskTest extends BaseAndroidTest
|
||||
for (Habit h : habitList) selected.add(h);
|
||||
|
||||
taskRunner.execute(
|
||||
new ExportCSVTask(habitList, selected, archiveFilename -> {
|
||||
new ExportCSVTask(testContext,habitList, selected, archiveFilename -> {
|
||||
assertThat(archiveFilename, is(not(nullValue())));
|
||||
File f = new File(archiveFilename);
|
||||
assertTrue(f.exists());
|
||||
|
||||
@@ -46,7 +46,7 @@ public class ExportDBTaskTest extends BaseAndroidTest
|
||||
@Test
|
||||
public void testExportCSV() throws Throwable
|
||||
{
|
||||
ExportDBTask task = new ExportDBTask(filename -> {
|
||||
ExportDBTask task = new ExportDBTask(testContext, filename -> {
|
||||
assertThat(filename, is(not(nullValue())));
|
||||
|
||||
File f = new File(filename);
|
||||
|
||||
@@ -38,7 +38,7 @@ import java.io.*;
|
||||
*/
|
||||
public class HabitsApplication extends Application
|
||||
{
|
||||
private static Context context;
|
||||
private Context context;
|
||||
|
||||
private static AppComponent component;
|
||||
|
||||
@@ -58,26 +58,14 @@ public class HabitsApplication extends Application
|
||||
HabitsApplication.component = component;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Deprecated
|
||||
public static Context getContext()
|
||||
{
|
||||
if (context == null) throw new RuntimeException("context is null");
|
||||
return context;
|
||||
}
|
||||
|
||||
public static boolean isTestMode()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (context != null)
|
||||
{
|
||||
String testClass = "org.isoron.uhabits.BaseAndroidTest";
|
||||
context.getClassLoader().loadClass(testClass);
|
||||
}
|
||||
Class.forName ("org.isoron.uhabits.BaseAndroidTest");
|
||||
return true;
|
||||
}
|
||||
catch (final Exception e)
|
||||
catch (final ClassNotFoundException e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -87,7 +75,7 @@ public class HabitsApplication extends Application
|
||||
public void onCreate()
|
||||
{
|
||||
super.onCreate();
|
||||
HabitsApplication.context = this;
|
||||
context = this;
|
||||
|
||||
component = DaggerAppComponent
|
||||
.builder()
|
||||
@@ -96,11 +84,11 @@ public class HabitsApplication extends Application
|
||||
|
||||
if (isTestMode())
|
||||
{
|
||||
File db = DatabaseUtils.getDatabaseFile();
|
||||
File db = DatabaseUtils.getDatabaseFile(context);
|
||||
if (db.exists()) db.delete();
|
||||
}
|
||||
|
||||
DatabaseUtils.initializeActiveAndroid();
|
||||
DatabaseUtils.initializeActiveAndroid(context);
|
||||
|
||||
widgetUpdater = component.getWidgetUpdater();
|
||||
widgetUpdater.startListening();
|
||||
@@ -125,7 +113,7 @@ public class HabitsApplication extends Application
|
||||
@Override
|
||||
public void onTerminate()
|
||||
{
|
||||
HabitsApplication.context = null;
|
||||
context = null;
|
||||
ActiveAndroid.dispose();
|
||||
|
||||
reminderScheduler.stopListening();
|
||||
|
||||
@@ -70,7 +70,7 @@ public class BaseSystem
|
||||
|
||||
if (context == null) throw new RuntimeException(
|
||||
"application context should not be null");
|
||||
File dir = FileUtils.getFilesDir("Logs");
|
||||
File dir = FileUtils.getFilesDir(context, "Logs");
|
||||
if (dir == null) throw new IOException("log dir should not be null");
|
||||
|
||||
File logFile =
|
||||
|
||||
@@ -41,6 +41,7 @@ import javax.inject.*;
|
||||
public class ListHabitsController
|
||||
implements HabitCardListController.HabitListener
|
||||
{
|
||||
|
||||
@NonNull
|
||||
private final ListHabitsScreen screen;
|
||||
|
||||
@@ -70,6 +71,8 @@ public class ListHabitsController
|
||||
|
||||
private ExportCSVTaskFactory exportCSVFactory;
|
||||
|
||||
private ExportDBTaskFactory exportDBFactory;
|
||||
|
||||
@Inject
|
||||
public ListHabitsController(@NonNull BaseSystem system,
|
||||
@NonNull CommandRunner commandRunner,
|
||||
@@ -82,7 +85,8 @@ public class ListHabitsController
|
||||
@NonNull WidgetUpdater widgetUpdater,
|
||||
@NonNull
|
||||
ImportDataTaskFactory importTaskFactory,
|
||||
@NonNull ExportCSVTaskFactory exportCSVFactory)
|
||||
@NonNull ExportCSVTaskFactory exportCSVFactory,
|
||||
@NonNull ExportDBTaskFactory exportDBFactory)
|
||||
{
|
||||
this.adapter = adapter;
|
||||
this.commandRunner = commandRunner;
|
||||
@@ -95,6 +99,7 @@ public class ListHabitsController
|
||||
this.widgetUpdater = widgetUpdater;
|
||||
this.importTaskFactory = importTaskFactory;
|
||||
this.exportCSVFactory = exportCSVFactory;
|
||||
this.exportDBFactory = exportDBFactory;
|
||||
}
|
||||
|
||||
public void onExportCSV()
|
||||
@@ -110,7 +115,7 @@ public class ListHabitsController
|
||||
|
||||
public void onExportDB()
|
||||
{
|
||||
taskRunner.execute(new ExportDBTask(filename -> {
|
||||
taskRunner.execute(exportDBFactory.create(filename -> {
|
||||
if (filename != null) screen.showSendFileScreen(filename);
|
||||
else screen.showMessage(R.string.could_not_export);
|
||||
}));
|
||||
|
||||
@@ -24,6 +24,8 @@ import android.content.*;
|
||||
import android.graphics.drawable.*;
|
||||
import android.os.*;
|
||||
import android.support.annotation.*;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.*;
|
||||
import android.widget.*;
|
||||
|
||||
|
||||
@@ -19,12 +19,14 @@
|
||||
|
||||
package org.isoron.uhabits.io;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.activeandroid.ActiveAndroid;
|
||||
|
||||
import org.isoron.uhabits.AppContext;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.utils.DatabaseUtils;
|
||||
import org.isoron.uhabits.utils.FileUtils;
|
||||
@@ -39,10 +41,14 @@ import javax.inject.*;
|
||||
*/
|
||||
public class LoopDBImporter extends AbstractImporter
|
||||
{
|
||||
@NonNull
|
||||
private Context context;
|
||||
|
||||
@Inject
|
||||
public LoopDBImporter(@NonNull HabitList habits)
|
||||
public LoopDBImporter(@NonNull @AppContext Context context, @NonNull HabitList habits)
|
||||
{
|
||||
super(habits);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,8 +74,8 @@ public class LoopDBImporter extends AbstractImporter
|
||||
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||
{
|
||||
ActiveAndroid.dispose();
|
||||
File originalDB = DatabaseUtils.getDatabaseFile();
|
||||
File originalDB = DatabaseUtils.getDatabaseFile(context);
|
||||
FileUtils.copy(file, originalDB);
|
||||
DatabaseUtils.initializeActiveAndroid();
|
||||
DatabaseUtils.initializeActiveAndroid(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ package org.isoron.uhabits.models.sqlite.records;
|
||||
import android.annotation.*;
|
||||
import android.database.*;
|
||||
import android.support.annotation.*;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.activeandroid.*;
|
||||
import com.activeandroid.annotation.*;
|
||||
|
||||
@@ -40,6 +40,9 @@ public class PebbleReceiver extends PebbleDataReceiver
|
||||
public static final UUID WATCHAPP_UUID =
|
||||
UUID.fromString("82629d99-8ea6-4631-a022-9ca77a12a058");
|
||||
|
||||
@NonNull
|
||||
private Context context;
|
||||
|
||||
private HabitList allHabits;
|
||||
|
||||
private CommandRunner commandRunner;
|
||||
@@ -61,6 +64,8 @@ public class PebbleReceiver extends PebbleDataReceiver
|
||||
if (context == null) throw new RuntimeException("context is null");
|
||||
if (data == null) throw new RuntimeException("data is null");
|
||||
|
||||
this.context = context;
|
||||
|
||||
HabitsApplication app =
|
||||
(HabitsApplication) context.getApplicationContext();
|
||||
|
||||
@@ -136,7 +141,7 @@ public class PebbleReceiver extends PebbleDataReceiver
|
||||
|
||||
private void sendDict(@NonNull PebbleDictionary dict)
|
||||
{
|
||||
PebbleKit.sendDataToPebble(HabitsApplication.getContext(),
|
||||
PebbleKit.sendDataToPebble(context,
|
||||
PebbleReceiver.WATCHAPP_UUID, dict);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,10 +19,13 @@
|
||||
|
||||
package org.isoron.uhabits.tasks;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import com.google.auto.factory.*;
|
||||
|
||||
import org.isoron.uhabits.AppContext;
|
||||
import org.isoron.uhabits.activities.ActivityContext;
|
||||
import org.isoron.uhabits.io.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
@@ -35,6 +38,9 @@ public class ExportCSVTask implements Task
|
||||
{
|
||||
private String archiveFilename;
|
||||
|
||||
@NonNull
|
||||
private final Context context;
|
||||
|
||||
@NonNull
|
||||
private final List<Habit> selectedHabits;
|
||||
|
||||
@@ -44,10 +50,12 @@ public class ExportCSVTask implements Task
|
||||
@NonNull
|
||||
private final HabitList habitList;
|
||||
|
||||
public ExportCSVTask(@Provided @NonNull HabitList habitList,
|
||||
public ExportCSVTask(@Provided @AppContext @NonNull Context context,
|
||||
@Provided @NonNull HabitList habitList,
|
||||
@NonNull List<Habit> selectedHabits,
|
||||
@NonNull Listener listener)
|
||||
{
|
||||
this.context = context;
|
||||
this.listener = listener;
|
||||
this.habitList = habitList;
|
||||
this.selectedHabits = selectedHabits;
|
||||
@@ -58,7 +66,7 @@ public class ExportCSVTask implements Task
|
||||
{
|
||||
try
|
||||
{
|
||||
File dir = FileUtils.getFilesDir("CSV");
|
||||
File dir = FileUtils.getFilesDir(context, "CSV");
|
||||
if (dir == null) return;
|
||||
|
||||
HabitsCSVExporter exporter;
|
||||
|
||||
@@ -19,22 +19,33 @@
|
||||
|
||||
package org.isoron.uhabits.tasks;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import com.google.auto.factory.AutoFactory;
|
||||
import com.google.auto.factory.Provided;
|
||||
|
||||
import org.isoron.uhabits.AppContext;
|
||||
import org.isoron.uhabits.activities.ActivityContext;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
@AutoFactory(allowSubclasses = true)
|
||||
public class ExportDBTask implements Task
|
||||
{
|
||||
private String filename;
|
||||
|
||||
@NonNull
|
||||
private Context context;
|
||||
|
||||
@NonNull
|
||||
private final Listener listener;
|
||||
|
||||
public ExportDBTask(@NonNull Listener listener)
|
||||
public ExportDBTask(@Provided @AppContext @NonNull Context context, @NonNull Listener listener)
|
||||
{
|
||||
this.listener = listener;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,10 +55,10 @@ public class ExportDBTask implements Task
|
||||
|
||||
try
|
||||
{
|
||||
File dir = FileUtils.getFilesDir("Backups");
|
||||
File dir = FileUtils.getFilesDir(context, "Backups");
|
||||
if (dir == null) return;
|
||||
|
||||
filename = DatabaseUtils.saveDatabaseCopy(dir);
|
||||
filename = DatabaseUtils.saveDatabaseCopy(context, dir);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
|
||||
@@ -47,9 +47,8 @@ public abstract class DatabaseUtils
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static File getDatabaseFile()
|
||||
public static File getDatabaseFile(Context context)
|
||||
{
|
||||
Context context = HabitsApplication.getContext();
|
||||
String databaseFilename = getDatabaseFilename();
|
||||
String root = context.getFilesDir().getPath();
|
||||
|
||||
@@ -68,9 +67,8 @@ public abstract class DatabaseUtils
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void initializeActiveAndroid()
|
||||
public static void initializeActiveAndroid(Context context)
|
||||
{
|
||||
Context context = HabitsApplication.getContext();
|
||||
Configuration dbConfig = new Configuration.Builder(context)
|
||||
.setDatabaseName(getDatabaseFilename())
|
||||
.setDatabaseVersion(BuildConfig.databaseVersion)
|
||||
@@ -82,14 +80,14 @@ public abstract class DatabaseUtils
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public static String saveDatabaseCopy(File dir) throws IOException
|
||||
public static String saveDatabaseCopy(Context context, File dir) throws IOException
|
||||
{
|
||||
SimpleDateFormat dateFormat = DateFormats.getBackupDateFormat();
|
||||
String date = dateFormat.format(DateUtils.getLocalTime());
|
||||
String format = "%s/Loop Habits Backup %s.db";
|
||||
String filename = String.format(format, dir.getAbsolutePath(), date);
|
||||
|
||||
File db = getDatabaseFile();
|
||||
File db = getDatabaseFile(context);
|
||||
File dbCopy = new File(filename);
|
||||
FileUtils.copy(db, dbCopy);
|
||||
|
||||
|
||||
@@ -87,9 +87,8 @@ public abstract class FileUtils
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getFilesDir(@Nullable String relativePath)
|
||||
public static File getFilesDir(@NonNull Context context, @Nullable String relativePath)
|
||||
{
|
||||
Context context = HabitsApplication.getContext();
|
||||
File externalFilesDirs[] =
|
||||
ContextCompat.getExternalFilesDirs(context, null);
|
||||
|
||||
|
||||
@@ -57,6 +57,8 @@ public class ListHabitsControllerTest extends BaseUnitTest
|
||||
|
||||
private ExportCSVTaskFactory exportCSVFactory;
|
||||
|
||||
private ExportDBTaskFactory exportDBFactory;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
{
|
||||
@@ -73,11 +75,12 @@ public class ListHabitsControllerTest extends BaseUnitTest
|
||||
widgetUpdater = mock(WidgetUpdater.class);
|
||||
importTaskFactory = mock(ImportDataTaskFactory.class);
|
||||
exportCSVFactory = mock(ExportCSVTaskFactory.class);
|
||||
exportDBFactory = mock(ExportDBTaskFactory.class);
|
||||
|
||||
controller =
|
||||
spy(new ListHabitsController(system, commandRunner, habitList,
|
||||
adapter, screen, prefs, reminderScheduler, taskRunner,
|
||||
widgetUpdater, importTaskFactory, exportCSVFactory));
|
||||
widgetUpdater, importTaskFactory, exportCSVFactory, exportDBFactory));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user