Commit 6d120c0e authored by Robert Hitt's avatar Robert Hitt

Merge branch 'settings' into 'master'

Settings

See merge request !8
parents 3482956e 5b721131
Pipeline #867 passed with stages
in 4 minutes and 34 seconds
......@@ -37,10 +37,10 @@ dependencies {
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.android.support:support-v4:25.1.0'
compile 'com.android.support:recyclerview-v7:25.1.0'
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:cardview-v7:25.1.1'
compile 'com.android.support:support-v4:25.1.1'
compile 'com.android.support:recyclerview-v7:25.1.1'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.4'
testCompile 'junit:junit:4.12'
......@@ -53,4 +53,5 @@ dependencies {
compile 'com.github.castorflex.smoothprogressbar:library-circular:1.2.0'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.4'
compile 'com.takisoft.fix:preference-v7:25.1.1.0'
}
......@@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:name=".MyApplication"
android:allowBackup="true"
......@@ -25,6 +26,7 @@
<activity android:name=".views.activities.DetailActivity"
android:parentActivityName=".views.activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".views.activities.MainActivity"/>
......@@ -32,11 +34,15 @@
<activity android:name=".views.activities.AboutActivity"
android:parentActivityName=".views.activities.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".views.activities.MainActivity"/>
</activity>
<activity android:name=".views.activities.SettingsActivity">
</activity>
<receiver android:name=".util.NotificationReceiver"
android:process=":remote" >
</receiver>
......
package srct.whatsopen;
import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import com.squareup.leakcanary.LeakCanary;
......@@ -24,4 +29,21 @@ public class MyApplication extends Application {
RealmConfiguration realmConfig = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(realmConfig);
}
public static void setRotation(Activity activity) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
boolean rotationOff = preferences.getBoolean("turn_off_rotation_preference",
false);
if(rotationOff) {
int currentOrientation = activity.getResources().getConfiguration().orientation;
if(currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
}
......@@ -4,6 +4,9 @@ import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.WakefulBroadcastReceiver;
......@@ -22,6 +25,13 @@ public class NotificationService extends IntentService {
String text = intent.getStringExtra("text");
int id = intent.getIntExtra("id", 0);
displayNotification(title, text, id);
setVibration();
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
private void displayNotification(String title, String text, int id) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_access_time_black_24dp)
......@@ -32,6 +42,16 @@ public class NotificationService extends IntentService {
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, builder.build());
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
private void setVibration() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean vibrationsOff = preferences.getBoolean("turn_off_vibrations_preference", false);
if(!vibrationsOff) {
Vibrator v = (Vibrator) this.getApplicationContext()
.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(400);
}
}
}
......@@ -11,6 +11,7 @@ import com.danielstone.materialaboutlibrary.model.MaterialAboutCard;
import com.danielstone.materialaboutlibrary.model.MaterialAboutList;
import com.danielstone.materialaboutlibrary.model.MaterialAboutTitleItem;
import srct.whatsopen.MyApplication;
import srct.whatsopen.R;
public class AboutActivity extends MaterialAboutActivity {
......@@ -20,6 +21,12 @@ public class AboutActivity extends MaterialAboutActivity {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
MyApplication.setRotation(this);
}
@Override
protected MaterialAboutList getMaterialAboutList() {
MaterialAboutCard.Builder appCardBuilder = new MaterialAboutCard.Builder();
......
package srct.whatsopen.views.activities;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
......@@ -23,6 +24,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.realm.Realm;
import srct.whatsopen.MyApplication;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
import srct.whatsopen.model.NotificationSettings;
......@@ -70,6 +72,12 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
setNotificationStatus();
}
@Override
protected void onResume() {
super.onResume();
MyApplication.setRotation(this);
}
@Override
protected void onDestroy() {
mPresenter.detachView();
......@@ -89,13 +97,11 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_favorite:
mPresenter.toggleFavorite(mFacility);
return true;
case R.id.action_settings:
expandSettingsActivity();
return true;
default:
return super.onOptionsItemSelected(item);
......@@ -176,4 +182,10 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
public void onSetNotification() {
setNotificationStatus();
}
// Opens the About page for the app
private void expandSettingsActivity() {
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
}
}
......@@ -2,9 +2,11 @@ package srct.whatsopen.views.activities;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
......@@ -19,6 +21,7 @@ import butterknife.ButterKnife;
import fr.castorflex.android.circularprogressbar.CircularProgressBar;
import io.realm.Realm;
import srct.whatsopen.MyApplication;
import srct.whatsopen.R;
import srct.whatsopen.views.MainView;
import srct.whatsopen.presenters.MainPresenter;
......@@ -50,15 +53,13 @@ public class MainActivity extends AppCompatActivity implements MainView {
Toolbar toolbar = ButterKnife.findById(this, R.id.toolbar);
setSupportActionBar(toolbar);
// Get the ViewPager and set its PagerAdapter
mViewPager.setAdapter(new FacilityListFragmentPagerAdapter(getSupportFragmentManager()));
// Now give the TabStrip the ViewPager
PagerSlidingTabStrip tabStrip = ButterKnife.findById(this, R.id.tabs);
tabStrip.setTabPaddingLeftRight(0);
tabStrip.setViewPager(mViewPager);
setUpTabStrip();
}
mViewPager.setCurrentItem(1);
@Override
protected void onResume() {
super.onResume();
MyApplication.setRotation(this);
}
@Override
......@@ -80,6 +81,7 @@ public class MainActivity extends AppCompatActivity implements MainView {
mPresenter.loadFacilities();
return true;
case R.id.action_settings:
expandSettingsActivity();
return true;
case R.id.action_about:
expandAboutActivity();
......@@ -89,6 +91,19 @@ public class MainActivity extends AppCompatActivity implements MainView {
}
}
private void setUpTabStrip() {
// Get the ViewPager and set its PagerAdapter
mViewPager.setAdapter(new FacilityListFragmentPagerAdapter(getSupportFragmentManager()));
// Now give the TabStrip the ViewPager
PagerSlidingTabStrip tabStrip = ButterKnife.findById(this, R.id.tabs);
tabStrip.setTabPaddingLeftRight(0);
tabStrip.setViewPager(mViewPager);
// Set the default tab to 'All'
mViewPager.setCurrentItem(1);
}
@Override
public void showProgressBar() {
mViewPager.setVisibility(View.INVISIBLE);
......@@ -111,4 +126,10 @@ public class MainActivity extends AppCompatActivity implements MainView {
Intent i = new Intent(this, AboutActivity.class);
startActivity(i);
}
// Opens the About page for the app
private void expandSettingsActivity() {
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
}
}
package srct.whatsopen.views.activities;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import butterknife.ButterKnife;
import srct.whatsopen.MyApplication;
import srct.whatsopen.R;
import srct.whatsopen.views.fragments.SettingsFragment;
public class SettingsActivity extends AppCompatActivity {
private SharedPreferences mSharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
configureToolbar();
getSupportFragmentManager().beginTransaction()
.replace(R.id.activity_settings, new SettingsFragment())
.commit();
setPreferenceChangeListener();
MyApplication.setRotation(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void configureToolbar() {
Toolbar toolbar = ButterKnife.findById(this, R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Settings");
// Display back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
private void setPreferenceChangeListener() {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.OnSharedPreferenceChangeListener prefListener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(key.equals("turn_off_rotation_preference")) {
MyApplication.setRotation(SettingsActivity.this);
}
}
};
mSharedPreferences.registerOnSharedPreferenceChangeListener(prefListener);
}
}
......@@ -3,8 +3,10 @@ package srct.whatsopen.views.adapters;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
......@@ -57,22 +59,16 @@ public class FacilityListAdapter extends
public void onBindViewHolder(ViewHolder holder, int position) {
Facility facility = getData().get(position);
displayStatusDurationText(facility, holder);
// highlight the open facilities
if(facility.isOpen()) {
// set the RV cell to be highlighted
holder.itemView.setBackgroundColor(ContextCompat
.getColor(context, R.color.facilityOpen));
// show the duration that the facility will be open
setItemPaddingInDp(holder.textLayout, 8);
holder.durationTextView.setVisibility(View.VISIBLE);
holder.nameTextView.setTypeface(null, Typeface.BOLD);
} else {
holder.itemView.setBackgroundColor(ContextCompat
.getColor(context, R.color.facilityClosed));
setItemPaddingInDp(holder.textLayout, 15);
holder.durationTextView.setVisibility(View.GONE);
holder.nameTextView.setTypeface(null, Typeface.NORMAL);
}
if(facility.isFavorited()) {
......@@ -87,6 +83,42 @@ public class FacilityListAdapter extends
textView.setText(facility.getName());
}
// Sets the duration text according to the user's settings
private void displayStatusDurationText(Facility facility, ViewHolder holder) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String setting = preferences.getString("list_view_information_preference",
"display_duration_both");
switch(setting) {
case "display_duration_open":
setStatusDurationText(holder, facility.isOpen());
break;
case "display_duration_closed":
setStatusDurationText(holder, !facility.isOpen());
break;
case "display_duration_none":
setStatusDurationText(holder, false);
break;
case "display_duration_both":default:
setStatusDurationText(holder, true);
break;
}
}
private void setStatusDurationText(ViewHolder holder, boolean showDuration) {
if(showDuration) {
// display the duration text
setItemPaddingInDp(holder.textLayout, 8);
holder.durationTextView.setVisibility(View.VISIBLE);
holder.nameTextView.setTypeface(null, Typeface.BOLD);
} else {
setItemPaddingInDp(holder.textLayout, 15);
holder.durationTextView.setVisibility(View.GONE);
holder.nameTextView.setTypeface(null, Typeface.NORMAL);
}
}
// Helper method to set the facility item layout's padding
// Have to convert from pixels to dp
private void setItemPaddingInDp(LinearLayout layout, int paddingPx) {
......
package srct.whatsopen.views.fragments;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
......@@ -32,6 +34,7 @@ public class FacilityListFragment extends android.support.v4.app.Fragment implem
private MainPresenter mPresenter;
private RecyclerView mRecyclerView;
private SwipeRefreshLayout mSwipeContainer;
private SharedPreferences.OnSharedPreferenceChangeListener mListener;
public static FacilityListFragment newInstance(String mode) {
Bundle args = new Bundle();
......@@ -50,6 +53,8 @@ public class FacilityListFragment extends android.support.v4.app.Fragment implem
mRealm = Realm.getDefaultInstance();
mPresenter = new MainPresenter();
mPresenter.attachView(this);
setPreferenceChangeListener();
}
@Override
......@@ -134,4 +139,19 @@ public class FacilityListFragment extends android.support.v4.app.Fragment implem
public Context getContext() {
return getActivity();
}
// Redraws RecyclerView if the settings have changed for it
private void setPreferenceChangeListener() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
mListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals("list_view_information_preference")) {
mRecyclerView.getAdapter().notifyDataSetChanged();
}
}
};
preferences.registerOnSharedPreferenceChangeListener(mListener);
}
}
package srct.whatsopen.views.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat;
import srct.whatsopen.R;
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferencesFix(@Nullable Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.settings, rootKey);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_settings">
<include
layout="@layout/toolbar_main"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
</menu>
\ No newline at end of file
......@@ -9,6 +9,7 @@
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorControlHighlight">@color/tabButtonPressed</item>
<item name="android:homeAsUpIndicator">@drawable/ic_arrow_back_white</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
</resources>
\ No newline at end of file
......@@ -2,4 +2,25 @@
<string name="app_name">What\'s Open</string>
<string name="toast_set_favorite">Added %s to Favorites</string>
<string name="toast_unset_favorite">Removed %s from Favorites</string>
<!-- Strings from PreferenceFragment -->
<string name="title_list_view">List View Preferences</string>
<string name="title_list_view_information">Duration display</string>
<string name="title_rotation">Rotation Preferences</string>
<string name="title_turn_off_rotation">Turn off rotation</string>
<string name="title_notification">Notification Preferences</string>
<string name="title_turn_off_vibrations">Turn off vibrations</string>
<string-array name="entries_list_view_information_preference">
<item>Both</item>
<item>Only for open Facilities</item>
<item>Only for closed Facilities</item>
<item>None</item>
</string-array>
<string-array name="entryvalues_list_view_information_preference">
<item>display_duration_both</item>
<item>display_duration_open</item>
<item>display_duration_closed</item>
<item>display_duration_none</item>
</string-array>
</resources>
......@@ -7,6 +7,7 @@
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:homeAsUpIndicator">@drawable/ic_arrow_back_white</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent" android:layout_width="match_parent">
<PreferenceCategory
android:title="@string/title_list_view">
<ListPreference
android:key="list_view_information_preference"
android:title="@string/title_list_view_information"
android:entries="@array/entries_list_view_information_preference"
android:entryValues="@array/entryvalues_list_view_information_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/title_rotation">
<CheckBoxPreference
android:key="turn_off_rotation_preference"
android:title="@string/title_turn_off_rotation" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/title_notification">
<CheckBoxPreference
android:key="turn_off_vibrations_preference"
android:title="@string/title_turn_off_vibrations" />
</PreferenceCategory>
</PreferenceScreen>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment