Skip to content

Commit

Permalink
Add option to specify station dao
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeemakr committed Aug 8, 2017
1 parent c8ac8d5 commit 83a3d8c
Show file tree
Hide file tree
Showing 25 changed files with 445 additions and 7 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ android {
vectorDrawables.useSupportLibrary = true
signingConfig file('../travis-release-key.jks').exists() ? signingConfigs.travisSigningConfig : null

javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}

}
buildTypes {
release {
Expand Down
10 changes: 10 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
# hide the original source file name.
#-renamesourcefileattribute SourceFile

-keepclassmembers class ch.unstable.ost.api.offline.StationsDAOFactory {
pubic *;
}
-keepclassmembers class ch.unstable.ost.api.search.StationsDAOFactory {
pubic *;
}
-keepclassmembers class ch.unstable.ost.api.transport.StationsDAOFactory {
pubic *;
}

-dontobfuscate


71 changes: 71 additions & 0 deletions app/schemas/ch.unstable.ost.api.offline.StationsDatabase/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "3b0a0f33592ccd09534143ac95e1f9d0",
"entities": [
{
"tableName": "stations",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`frequency` INTEGER, `name` TEXT, `id` TEXT, `types` INTEGER, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "frequency",
"columnName": "frequency",
"affinity": "INTEGER"
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT"
},
{
"fieldPath": "id",
"columnName": "id",
"affinity": "TEXT"
},
{
"fieldPath": "types",
"columnName": "types",
"affinity": "INTEGER"
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "fts_stations",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`docid` INTEGER, `fts_stations` INTEGER, PRIMARY KEY(`docid`))",
"fields": [
{
"fieldPath": "docid",
"columnName": "docid",
"affinity": "INTEGER"
},
{
"fieldPath": "fts_stations",
"columnName": "fts_stations",
"affinity": "INTEGER"
}
],
"primaryKey": {
"columnNames": [
"docid"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"3b0a0f33592ccd09534143ac95e1f9d0\")"
]
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/ch/unstable/ost/ChooseStationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import ch.unstable.ost.api.transport.TransportAPI;
import ch.unstable.ost.database.Databases;
import ch.unstable.ost.preference.SettingsActivity;
import ch.unstable.ost.preference.StationDaoLoader;
import ch.unstable.ost.theme.ThemedActivity;

public class ChooseStationActivity extends ThemedActivity {
Expand Down Expand Up @@ -78,8 +79,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

if(stationsDAO == null) {
StationsDatabase database = Databases.getStationsDatabase(this);
stationsDAO = database.getStationsDAO();
stationsDAO = StationDaoLoader.createStationDAO(this);
}
mLocationResultAdapter = new StationListAdapter(this);
mLocationResultAdapter.setOnStationClickListener(new StationListAdapter.OnStationClickListener() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ch.unstable.ost.api.offline;

import android.content.Context;
import android.support.annotation.NonNull;

import ch.unstable.ost.api.transport.TransportAPI;
import ch.unstable.ost.database.Databases;
import ch.unstable.ost.preference.StationDaoLoader;

public class StationsDAOFactory implements StationDaoLoader.StationDAOFactory {
@NonNull
@Override
public OfflineStationsDAO getStationsDAO(@NonNull Context context) {
StationsDatabase database = Databases.getStationsDatabase(context);
return database.getStationsDAO();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@TypeConverters(value = {StationTypeConverter.class})
@Database(entities = {LocationEntity.class, LocationEntityFTS.class}, version = 1)
public abstract class StationsDatabase extends RoomDatabase{
public abstract class StationsDatabase extends RoomDatabase {

public abstract OfflineStationsDAO getStationsDAO();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ch.unstable.ost.api.search;


import android.content.Context;
import android.support.annotation.NonNull;

import ch.unstable.ost.preference.StationDaoLoader;

public class StationsDAOFactory implements StationDaoLoader.StationDAOFactory {
@NonNull
@Override
public SearchAPI getStationsDAO(@NonNull Context context) {
return new SearchAPI();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ch.unstable.ost.api.transport;

import android.content.Context;
import android.support.annotation.NonNull;

import ch.unstable.ost.preference.StationDaoLoader;

public class StationsDAOFactory implements StationDaoLoader.StationDAOFactory {
@NonNull
@Override
public TransportAPI getStationsDAO(@NonNull Context context) {
return new TransportAPI();
}
}
1 change: 1 addition & 0 deletions app/src/main/java/ch/unstable/ost/database/Databases.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.OutputStream;

import ch.unstable.ost.api.offline.StationsDatabase;
import ch.unstable.ost.preference.StationDaoLoader;

public class Databases {
private static final boolean FORCE_OVERRIDE = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ch.unstable.ost.preference;

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;

import ch.unstable.ost.R;


public class ContentSettingsFragment extends PreferenceFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

public class PreferenceKeys {
public static final String KEY_THEME = "pref_theme";
public static final String KEY_STATIONS_DAO = "pref_station_dao";

private PreferenceKeys() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDelegate;

import java.util.List;

Expand All @@ -11,6 +12,10 @@

public class SettingsActivity extends PreferenceActivity implements AppearanceSettingsFragment.OnThemeChangedListener {

static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
ThemeHelper.setTheme(this);
Expand All @@ -30,6 +35,7 @@ public void onThemeChanged() {

@Override
protected boolean isValidFragment(String fragmentName) {
return AppearanceSettingsFragment.class.getName().equals(fragmentName);
return AppearanceSettingsFragment.class.getName().equals(fragmentName)
|| ContentSettingsFragment.class.getName().equals(fragmentName);
}
}
31 changes: 31 additions & 0 deletions app/src/main/java/ch/unstable/ost/preference/StationDaoLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ch.unstable.ost.preference;


import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;

import ch.unstable.ost.R;
import ch.unstable.ost.api.StationsDAO;

public class StationDaoLoader {
public interface StationDAOFactory {
@NonNull
StationsDAO getStationsDAO(@NonNull Context context);
}

@NonNull
public static StationsDAO createStationDAO(final Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String preference = sharedPreferences.getString(PreferenceKeys.KEY_STATIONS_DAO, context.getString(R.string.prefs_station_dao_factory_default));

try {
Class<?> clazz = Class.forName(preference);
StationDAOFactory daoFactory = (StationDAOFactory) clazz.newInstance();
return daoFactory.getStationsDAO(context);
} catch (Exception e) {
throw new IllegalStateException("Couldn't get factory for StationsDAO", e);
}
}
}
11 changes: 9 additions & 2 deletions app/src/main/java/ch/unstable/ost/theme/ThemeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@
import android.support.annotation.AttrRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StyleRes;
import android.util.Log;
import android.util.TypedValue;

import ch.unstable.ost.R;
import ch.unstable.ost.preference.PreferenceKeys;

public class ThemeHelper {

private static final String TAG = "ThemeHelper";

@StyleRes
private static final int DEFAULT_THEME = R.style.GreenDarkTheme;

public static void setTheme(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String currentTheme = preferences.getString(PreferenceKeys.KEY_THEME, null);
int style;
if(currentTheme == null) {
style = R.style.GreenDarkTheme;
style = DEFAULT_THEME;
} else {
style = getThemeStyle(context.getResources(), currentTheme);
}
Expand All @@ -46,7 +52,8 @@ private static int getThemeStyle(Resources resources, String currentTheme) {
}
styles.recycle();
if (styleRes == 0) {
throw new IllegalStateException("style not found (currentTheme: " + currentTheme + ")");
Log.e(TAG, "style not found (currentTheme: " + currentTheme + ")");
return DEFAULT_THEME;
}
return styleRes;
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_color_lens_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,3c-4.97,0 -9,4.03 -9,9s4.03,9 9,9c0.83,0 1.5,-0.67 1.5,-1.5 0,-0.39 -0.15,-0.74 -0.39,-1.01 -0.23,-0.26 -0.38,-0.61 -0.38,-0.99 0,-0.83 0.67,-1.5 1.5,-1.5L16,16c2.76,0 5,-2.24 5,-5 0,-4.42 -4.03,-8 -9,-8zM6.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S5.67,9 6.5,9 8,9.67 8,10.5 7.33,12 6.5,12zM9.5,8C8.67,8 8,7.33 8,6.5S8.67,5 9.5,5s1.5,0.67 1.5,1.5S10.33,8 9.5,8zM14.5,8c-0.83,0 -1.5,-0.67 -1.5,-1.5S13.67,5 14.5,5s1.5,0.67 1.5,1.5S15.33,8 14.5,8zM17.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S16.67,9 17.5,9s1.5,0.67 1.5,1.5 -0.67,1.5 -1.5,1.5z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_color_lens_black_24dp"/>
</selector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_color_lens_white_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M12,3c-4.97,0 -9,4.03 -9,9s4.03,9 9,9c0.83,0 1.5,-0.67 1.5,-1.5 0,-0.39 -0.15,-0.74 -0.39,-1.01 -0.23,-0.26 -0.38,-0.61 -0.38,-0.99 0,-0.83 0.67,-1.5 1.5,-1.5L16,16c2.76,0 5,-2.24 5,-5 0,-4.42 -4.03,-8 -9,-8zM6.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S5.67,9 6.5,9 8,9.67 8,10.5 7.33,12 6.5,12zM9.5,8C8.67,8 8,7.33 8,6.5S8.67,5 9.5,5s1.5,0.67 1.5,1.5S10.33,8 9.5,8zM14.5,8c-0.83,0 -1.5,-0.67 -1.5,-1.5S13.67,5 14.5,5s1.5,0.67 1.5,1.5S15.33,8 14.5,8zM17.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S16.67,9 17.5,9s1.5,0.67 1.5,1.5 -0.67,1.5 -1.5,1.5z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_color_lens_white_24dp"/>
</selector>
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@
<string name="departure_time_same_day">Abfahrt um %1$s Uhr</string>
<string name="time_restriction_arrival">Ankunft</string>
<string name="time_restriction_departure">Abfahrt</string>
<string name="prefs_category_content">Inhalt</string>
<string name="station_dao_offline">Offline</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<attr name="ic_send_24dp" format="reference" />
<attr name="ic_tune_24dp_no_vector" format="reference" />
<attr name="ic_restore_24dp" format="reference" />

<attr name="ic_color_lens_24dp_no_vector" format="reference" />

<attr name="toolbarStyle" format="reference" />
<attr name="toolbarTheme" format="reference" />
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,27 @@
<string name="time_restriction_arrival">Arrival</string>
<string name="time_restriction_departure">Departure</string>


<string name="pref_station_dao_factory_title">Station Datasource</string>
<string name="pref_station_dao_factory_summary">The data source for station autocompletion</string>

<string name="station_dao_factory_offline" translatable="false">ch.unstable.ost.api.offline.StationsDAOFactory</string>
<string name="station_dao_factory_search_ch" translatable="false">ch.unstable.ost.api.search.StationsDAOFactory</string>
<string name="station_dao_factory_transport_ch" translatable="false">ch.unstable.ost.api.transport.StationsDAOFactory</string>
<string-array name="prefs_station_dao_factory_values">
<item>@string/station_dao_factory_offline</item>
<item>@string/station_dao_factory_search_ch</item>
<item>@string/station_dao_factory_transport_ch</item>
</string-array>
<string-array name="prefs_station_dao_factory_names">
<item>@string/station_dao_offline</item>
<item>@string/station_dao_search_ch</item>
<item>@string/station_dao_transport_opendata_ch</item>
</string-array>
<string name="prefs_station_dao_factory_default" translatable="false">@string/station_dao_factory_offline</string>
<string name="prefs_category_content">Content</string>
<string name="station_dao_offline">Offline</string>
<string name="station_dao_search_ch" translatable="false">timetable.search.ch</string>
<string name="station_dao_transport_opendata_ch" translatable="false">transport.opendata.ch</string>

</resources>
Loading

0 comments on commit 83a3d8c

Please sign in to comment.