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 { ...@@ -37,10 +37,10 @@ dependencies {
compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.android.support:appcompat-v7:25.1.0' compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:cardview-v7:25.1.0' compile 'com.android.support:cardview-v7:25.1.1'
compile 'com.android.support:support-v4:25.1.0' compile 'com.android.support:support-v4:25.1.1'
compile 'com.android.support:recyclerview-v7:25.1.0' compile 'com.android.support:recyclerview-v7:25.1.1'
compile 'io.reactivex:rxandroid:1.2.0' compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.4' compile 'io.reactivex:rxjava:1.1.4'
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
...@@ -53,4 +53,5 @@ dependencies { ...@@ -53,4 +53,5 @@ dependencies {
compile 'com.github.castorflex.smoothprogressbar:library-circular:1.2.0' compile 'com.github.castorflex.smoothprogressbar:library-circular:1.2.0'
compile 'io.reactivex:rxandroid:1.2.0' compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.4' compile 'io.reactivex:rxjava:1.1.4'
compile 'com.takisoft.fix:preference-v7:25.1.1.0'
} }
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application <application
android:name=".MyApplication" android:name=".MyApplication"
android:allowBackup="true" android:allowBackup="true"
...@@ -25,6 +26,7 @@ ...@@ -25,6 +26,7 @@
<activity android:name=".views.activities.DetailActivity" <activity android:name=".views.activities.DetailActivity"
android:parentActivityName=".views.activities.MainActivity"> android:parentActivityName=".views.activities.MainActivity">
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value=".views.activities.MainActivity"/> android:value=".views.activities.MainActivity"/>
...@@ -32,11 +34,15 @@ ...@@ -32,11 +34,15 @@
<activity android:name=".views.activities.AboutActivity" <activity android:name=".views.activities.AboutActivity"
android:parentActivityName=".views.activities.MainActivity"> android:parentActivityName=".views.activities.MainActivity">
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value=".views.activities.MainActivity"/> android:value=".views.activities.MainActivity"/>
</activity> </activity>
<activity android:name=".views.activities.SettingsActivity">
</activity>
<receiver android:name=".util.NotificationReceiver" <receiver android:name=".util.NotificationReceiver"
android:process=":remote" > android:process=":remote" >
</receiver> </receiver>
......
package srct.whatsopen; package srct.whatsopen;
import android.app.Activity;
import android.app.Application; 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; import com.squareup.leakcanary.LeakCanary;
...@@ -24,4 +29,21 @@ public class MyApplication extends Application { ...@@ -24,4 +29,21 @@ public class MyApplication extends Application {
RealmConfiguration realmConfig = new RealmConfiguration.Builder().build(); RealmConfiguration realmConfig = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(realmConfig); 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; ...@@ -4,6 +4,9 @@ import android.app.IntentService;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; 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.app.NotificationCompat;
import android.support.v4.content.WakefulBroadcastReceiver; import android.support.v4.content.WakefulBroadcastReceiver;
...@@ -22,16 +25,33 @@ public class NotificationService extends IntentService { ...@@ -22,16 +25,33 @@ public class NotificationService extends IntentService {
String text = intent.getStringExtra("text"); String text = intent.getStringExtra("text");
int id = intent.getIntExtra("id", 0); 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 = NotificationCompat.Builder builder =
new NotificationCompat.Builder(this) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_access_time_black_24dp) .setSmallIcon(R.drawable.ic_access_time_black_24dp)
.setContentTitle(title) .setContentTitle(title)
.setContentText(text); .setContentText(text);
NotificationManager notificationManager = (NotificationManager) NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE); getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, builder.build()); 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; ...@@ -11,6 +11,7 @@ import com.danielstone.materialaboutlibrary.model.MaterialAboutCard;
import com.danielstone.materialaboutlibrary.model.MaterialAboutList; import com.danielstone.materialaboutlibrary.model.MaterialAboutList;
import com.danielstone.materialaboutlibrary.model.MaterialAboutTitleItem; import com.danielstone.materialaboutlibrary.model.MaterialAboutTitleItem;
import srct.whatsopen.MyApplication;
import srct.whatsopen.R; import srct.whatsopen.R;
public class AboutActivity extends MaterialAboutActivity { public class AboutActivity extends MaterialAboutActivity {
...@@ -20,6 +21,12 @@ public class AboutActivity extends MaterialAboutActivity { ...@@ -20,6 +21,12 @@ public class AboutActivity extends MaterialAboutActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
} }
@Override
protected void onResume() {
super.onResume();
MyApplication.setRotation(this);
}
@Override @Override
protected MaterialAboutList getMaterialAboutList() { protected MaterialAboutList getMaterialAboutList() {
MaterialAboutCard.Builder appCardBuilder = new MaterialAboutCard.Builder(); MaterialAboutCard.Builder appCardBuilder = new MaterialAboutCard.Builder();
......
package srct.whatsopen.views.activities; package srct.whatsopen.views.activities;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
...@@ -23,6 +24,7 @@ import butterknife.BindView; ...@@ -23,6 +24,7 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
import io.realm.Realm; import io.realm.Realm;
import srct.whatsopen.MyApplication;
import srct.whatsopen.R; import srct.whatsopen.R;
import srct.whatsopen.model.Facility; import srct.whatsopen.model.Facility;
import srct.whatsopen.model.NotificationSettings; import srct.whatsopen.model.NotificationSettings;
...@@ -70,6 +72,12 @@ public class DetailActivity extends AppCompatActivity implements FacilityView, ...@@ -70,6 +72,12 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
setNotificationStatus(); setNotificationStatus();
} }
@Override
protected void onResume() {
super.onResume();
MyApplication.setRotation(this);
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
mPresenter.detachView(); mPresenter.detachView();
...@@ -89,13 +97,11 @@ public class DetailActivity extends AppCompatActivity implements FacilityView, ...@@ -89,13 +97,11 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_favorite: case R.id.action_favorite:
mPresenter.toggleFavorite(mFacility); mPresenter.toggleFavorite(mFacility);
return true; return true;
case R.id.action_settings: case R.id.action_settings:
expandSettingsActivity();
return true; return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
...@@ -176,4 +182,10 @@ public class DetailActivity extends AppCompatActivity implements FacilityView, ...@@ -176,4 +182,10 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
public void onSetNotification() { public void onSetNotification() {
setNotificationStatus(); 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; ...@@ -2,9 +2,11 @@ package srct.whatsopen.views.activities;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
...@@ -19,6 +21,7 @@ import butterknife.ButterKnife; ...@@ -19,6 +21,7 @@ import butterknife.ButterKnife;
import fr.castorflex.android.circularprogressbar.CircularProgressBar; import fr.castorflex.android.circularprogressbar.CircularProgressBar;
import io.realm.Realm; import io.realm.Realm;
import srct.whatsopen.MyApplication;
import srct.whatsopen.R; import srct.whatsopen.R;
import srct.whatsopen.views.MainView; import srct.whatsopen.views.MainView;
import srct.whatsopen.presenters.MainPresenter; import srct.whatsopen.presenters.MainPresenter;
...@@ -50,15 +53,13 @@ public class MainActivity extends AppCompatActivity implements MainView { ...@@ -50,15 +53,13 @@ public class MainActivity extends AppCompatActivity implements MainView {
Toolbar toolbar = ButterKnife.findById(this, R.id.toolbar); Toolbar toolbar = ButterKnife.findById(this, R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
// Get the ViewPager and set its PagerAdapter setUpTabStrip();
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);
mViewPager.setCurrentItem(1); @Override
protected void onResume() {
super.onResume();
MyApplication.setRotation(this);
} }
@Override @Override
...@@ -80,6 +81,7 @@ public class MainActivity extends AppCompatActivity implements MainView { ...@@ -80,6 +81,7 @@ public class MainActivity extends AppCompatActivity implements MainView {
mPresenter.loadFacilities(); mPresenter.loadFacilities();
return true; return true;
case R.id.action_settings: case R.id.action_settings:
expandSettingsActivity();
return true; return true;
case R.id.action_about: case R.id.action_about:
expandAboutActivity(); expandAboutActivity();
...@@ -89,6 +91,19 @@ public class MainActivity extends AppCompatActivity implements MainView { ...@@ -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 @Override
public void showProgressBar() { public void showProgressBar() {
mViewPager.setVisibility(View.INVISIBLE); mViewPager.setVisibility(View.INVISIBLE);
...@@ -111,4 +126,10 @@ public class MainActivity extends AppCompatActivity implements MainView { ...@@ -111,4 +126,10 @@ public class MainActivity extends AppCompatActivity implements MainView {
Intent i = new Intent(this, AboutActivity.class); Intent i = new Intent(this, AboutActivity.class);
startActivity(i); 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; ...@@ -3,8 +3,10 @@ package srct.whatsopen.views.adapters;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -57,22 +59,16 @@ public class FacilityListAdapter extends ...@@ -57,22 +59,16 @@ public class FacilityListAdapter extends
public void onBindViewHolder(ViewHolder holder, int position) { public void onBindViewHolder(ViewHolder holder, int position) {
Facility facility = getData().get(position); Facility facility = getData().get(position);
displayStatusDurationText(facility, holder);
// highlight the open facilities
if(facility.isOpen()) { if(facility.isOpen()) {
// set the RV cell to be highlighted // set the RV cell to be highlighted
holder.itemView.setBackgroundColor(ContextCompat holder.itemView.setBackgroundColor(ContextCompat
.getColor(context, R.color.facilityOpen)); .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 { } else {
holder.itemView.setBackgroundColor(ContextCompat holder.itemView.setBackgroundColor(ContextCompat
.getColor(context, R.color.facilityClosed)); .getColor(context, R.color.facilityClosed));
setItemPaddingInDp(holder.textLayout, 15);
holder.durationTextView.setVisibility(View.GONE);
holder.nameTextView.setTypeface(null, Typeface.NORMAL);
} }
if(facility.isFavorited()) { if(facility.isFavorited()) {
...@@ -87,6 +83,42 @@ public class FacilityListAdapter extends ...@@ -87,6 +83,42 @@ public class FacilityListAdapter extends
textView.setText(facility.getName()); 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 // Helper method to set the facility item layout's padding
// Have to convert from pixels to dp // Have to convert from pixels to dp
private void setItemPaddingInDp(LinearLayout layout, int paddingPx) { private void setItemPaddingInDp(LinearLayout layout, int paddingPx) {
......
package srct.whatsopen.views.fragments; package srct.whatsopen.views.fragments;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -32,6 +34,7 @@ public class FacilityListFragment extends android.support.v4.app.Fragment implem ...@@ -32,6 +34,7 @@ public class FacilityListFragment extends android.support.v4.app.Fragment implem
private MainPresenter mPresenter; private MainPresenter mPresenter;
private RecyclerView mRecyclerView; private RecyclerView mRecyclerView;
private SwipeRefreshLayout mSwipeContainer; private SwipeRefreshLayout mSwipeContainer;
private SharedPreferences.OnSharedPreferenceChangeListener mListener;