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