Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2014 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.wearable.speedtracker.ui;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.content.SharedPreferences;
     22 import android.os.Bundle;
     23 import android.preference.PreferenceManager;
     24 import android.view.View;
     25 import android.widget.TextView;
     26 
     27 import com.example.android.wearable.speedtracker.R;
     28 
     29 /**
     30  * A simple activity that allows the user to start or stop recording of GPS location data.
     31  */
     32 public class LocationSettingActivity extends Activity {
     33 
     34     private static final String PREFS_KEY_SAVE_GPS = "save-gps";
     35 
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39         setContentView(R.layout.saving_activity);
     40         TextView textView = (TextView) findViewById(R.id.textView);
     41         textView.setText(getGpsRecordingStatusFromPreferences(this) ? R.string.stop_saving_gps
     42                 : R.string.start_saving_gps);
     43 
     44     }
     45 
     46     public void onClick(View view) {
     47         switch (view.getId()) {
     48             case R.id.submitBtn:
     49                 saveGpsRecordingStatusToPreferences(LocationSettingActivity.this,
     50                         !getGpsRecordingStatusFromPreferences(this));
     51                 break;
     52             case R.id.cancelBtn:
     53                 break;
     54         }
     55         finish();
     56     }
     57 
     58     /**
     59      * Get the persisted value for whether the app should record the GPS location data or not. If
     60      * there is no prior value persisted, it returns {@code false}.
     61      */
     62     public static boolean getGpsRecordingStatusFromPreferences(Context context) {
     63         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
     64         return pref.getBoolean(PREFS_KEY_SAVE_GPS, false);
     65     }
     66 
     67     /**
     68      * Persists the user selection to whether save the GPS location data or not.
     69      */
     70     public static void saveGpsRecordingStatusToPreferences(Context context, boolean value) {
     71         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
     72         pref.edit().putBoolean(PREFS_KEY_SAVE_GPS, value).apply();
     73 
     74     }
     75 }
     76