• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.jumpingjack.fragments;
18 
19 import com.example.android.wearable.jumpingjack.R;
20 import com.example.android.wearable.jumpingjack.Utils;
21 
22 import android.app.Fragment;
23 import android.graphics.drawable.Drawable;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.TextView;
30 
31 import java.util.Timer;
32 import java.util.TimerTask;
33 
34 /**
35  * A simple fragment for showing the count
36  */
37 public class CounterFragment extends Fragment {
38 
39     private static final long ANIMATION_INTERVAL_MS = 500; // in milliseconds
40     private TextView mCounterText;
41     private Timer mAnimationTimer;
42     private Handler mHandler;
43     private TimerTask mAnimationTask;
44     private boolean up = false;
45     private Drawable mDownDrawable;
46     private Drawable mUpDrawable;
47 
48     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)49     public View onCreateView(LayoutInflater inflater, ViewGroup container,
50             Bundle savedInstanceState) {
51         View view = inflater.inflate(R.layout.counter_layout, container, false);
52         mDownDrawable = getResources().getDrawable(R.drawable.jump_down_50);
53         mUpDrawable = getResources().getDrawable(R.drawable.jump_up_50);
54         mCounterText = (TextView) view.findViewById(R.id.counter);
55         mCounterText.setCompoundDrawablesWithIntrinsicBounds(mUpDrawable, null, null, null);
56         setCounter(Utils.getCounterFromPreference(getActivity()));
57         mHandler = new Handler();
58         startAnimation();
59         return view;
60     }
61 
startAnimation()62     private void startAnimation() {
63         mAnimationTask = new TimerTask() {
64             @Override
65             public void run() {
66                 mHandler.post(new Runnable() {
67                     @Override
68                     public void run() {
69                         mCounterText.setCompoundDrawablesWithIntrinsicBounds(
70                                 up ? mUpDrawable : mDownDrawable, null, null, null);
71                         up = !up;
72                     }
73                 });
74             }
75         };
76         mAnimationTimer = new Timer();
77         mAnimationTimer.scheduleAtFixedRate(mAnimationTask, ANIMATION_INTERVAL_MS,
78                 ANIMATION_INTERVAL_MS);
79     }
80 
setCounter(String text)81     public void setCounter(String text) {
82         mCounterText.setText(text);
83     }
84 
setCounter(int i)85     public void setCounter(int i) {
86         setCounter(i < 0 ? "0" : String.valueOf(i));
87     }
88 
89     @Override
90     public void onDetach() {
91         mAnimationTimer.cancel();
92         super.onDetach();
93     }
94 }
95