Home | History | Annotate | Download | only in speedtracker
      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;
     18 
     19 import android.app.Activity;
     20 import android.content.SharedPreferences;
     21 import android.os.Bundle;
     22 import android.preference.PreferenceManager;
     23 import android.support.wearable.view.WearableListView;
     24 import android.widget.TextView;
     25 
     26 import com.example.android.wearable.speedtracker.ui.SpeedPickerListAdapter;
     27 
     28 /**
     29  * An activity that presents a list of speeds to user and allows user to pick one, to be used as
     30  * the current speed limit.
     31  */
     32 public class SpeedPickerActivity extends Activity implements WearableListView.ClickListener {
     33 
     34     /* Speeds, in mph, that will be shown on the list */
     35     private int[] speeds = {25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75};
     36 
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40         setContentView(R.layout.speed_picker_activity);
     41 
     42         final TextView header = (TextView) findViewById(R.id.header);
     43 
     44         // Get the list component from the layout of the activity
     45         WearableListView listView = (WearableListView) findViewById(R.id.wearable_list);
     46 
     47         // Assign an adapter to the list
     48         listView.setAdapter(new SpeedPickerListAdapter(this, speeds));
     49 
     50         // Set a click listener
     51         listView.setClickListener(this);
     52 
     53         listView.addOnScrollListener(new WearableListView.OnScrollListener() {
     54             @Override
     55             public void onScroll(int i) {
     56             }
     57 
     58             @Override
     59             public void onAbsoluteScrollChange(int i) {
     60                 // only scroll the header up from the base position, not down...
     61                 if (i > 0) {
     62                     header.setY(-i);
     63                 }
     64             }
     65 
     66             @Override
     67             public void onScrollStateChanged(int i) {
     68             }
     69 
     70             @Override
     71             public void onCentralPositionChanged(int i) {
     72             }
     73         });
     74     }
     75 
     76     @Override
     77     public void onClick(WearableListView.ViewHolder viewHolder) {
     78         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
     79         pref.edit().putInt(WearableMainActivity.PREFS_SPEED_LIMIT_KEY,
     80                 speeds[viewHolder.getPosition()]).apply();
     81         finish();
     82     }
     83 
     84     @Override
     85     public void onTopEmptyRegionClick() {
     86     }
     87 }
     88