Commit 4c5d51d5 authored by Robert Hitt's avatar Robert Hitt

Rotation can now be turned off

parent 33d1b17e
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);
}
}
}
......@@ -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();
......
......@@ -24,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;
......@@ -71,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();
......
......@@ -19,6 +19,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;
......@@ -61,6 +62,12 @@ public class MainActivity extends AppCompatActivity implements MainView {
mViewPager.setCurrentItem(1);
}
@Override
protected void onResume() {
super.onResume();
MyApplication.setRotation(this);
}
@Override
protected void onDestroy() {
mPresenter.detachView();
......
package srct.whatsopen.views.activities;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NavUtils;
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);
......@@ -25,8 +29,11 @@ public class SettingsActivity extends AppCompatActivity {
getSupportFragmentManager().beginTransaction()
.replace(R.id.activity_settings, new SettingsFragment())
.commit();
}
setPreferenceChangeListener();
MyApplication.setRotation(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
......@@ -56,5 +63,20 @@ public class SettingsActivity extends AppCompatActivity {
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);
}
}
package srct.whatsopen.views.fragments;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat;
import java.util.List;
import srct.whatsopen.R;
public class SettingsFragment extends PreferenceFragmentCompat {
//implements SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences mSharedPreferences;
@Override
public void onCreatePreferencesFix(@Nullable Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.settings, rootKey);
//mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
}
}
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