Home | History | Annotate | Download | only in wearaccessibilityapp
      1 /*
      2  * Copyright (C) 2017 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 package com.example.android.wearable.wear.wearaccessibilityapp;
     17 
     18 import android.content.Context;
     19 import android.os.CountDownTimer;
     20 import android.preference.Preference;
     21 import android.support.wearable.view.CircledImageView;
     22 import android.util.AttributeSet;
     23 import android.util.TypedValue;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.TextView;
     28 
     29 public class CircledImageViewPreference extends Preference {
     30 
     31     private CircledImageView mCircledImage;
     32     private TextView mCircledImageText;
     33     private CountDownTimer mCountDownTimer;
     34     private int mColorPrimaryDark;
     35     private int mColorAccent;
     36     private boolean mIsLoading;
     37     private Context mContext;
     38 
     39     public CircledImageViewPreference(Context context, AttributeSet attrs) {
     40         super(context, attrs);
     41         mContext = context;
     42 
     43         TypedValue typedValue = new TypedValue();
     44         mContext.getTheme().resolveAttribute(android.R.attr.colorPrimaryDark, typedValue, true);
     45         mColorPrimaryDark = typedValue.data;
     46         mContext.getTheme().resolveAttribute(android.R.attr.colorAccent, typedValue, true);
     47         mColorAccent = typedValue.data;
     48     }
     49 
     50     @Override
     51     protected View onCreateView(ViewGroup parent) {
     52         super.onCreateView(parent);
     53         LayoutInflater inflater = LayoutInflater.from(getContext());
     54         View circledImageViewLayout = inflater.inflate(R.layout.circled_image_layout, null);
     55 
     56         mCircledImage = circledImageViewLayout.findViewById(R.id.circled_image_view);
     57         mCircledImageText = circledImageViewLayout.findViewById(R.id.circled_image_text);
     58 
     59         setStartCircledImageView();
     60         return circledImageViewLayout;
     61     }
     62 
     63     @Override
     64     protected void onClick() {
     65         mIsLoading = !mIsLoading;
     66         if (mIsLoading) {
     67             setLoadingCircledImageView();
     68         } else {
     69             setStartCircledImageView();
     70             cancelCountDownTimer();
     71         }
     72     }
     73 
     74     public void setStartCircledImageView() {
     75         mCircledImageText.setText(R.string.start);
     76 
     77         mCircledImage.setImageResource(R.drawable.start);
     78         mCircledImage.setCircleBorderColor(mColorPrimaryDark);
     79         mCircledImage.setProgress(1);
     80 
     81         mIsLoading = false;
     82     }
     83 
     84     public void setLoadingCircledImageView() {
     85         mCircledImageText.setText(R.string.loading);
     86         mCircledImage.setImageResource(R.drawable.stop);
     87         mCircledImage.setCircleBorderColor(mColorAccent);
     88         mCountDownTimer =
     89                 new CountDownTimer(10000, 10) {
     90                     @Override
     91                     public void onTick(long millisUntilFinished) {
     92                         float timeElapsed = (10000.0f - millisUntilFinished) / 10000.0f;
     93                         mCircledImage.setProgress(timeElapsed);
     94                     }
     95 
     96                     @Override
     97                     public void onFinish() {
     98                         setStartCircledImageView();
     99                     }
    100                 }.start();
    101     }
    102 
    103     public void cancelCountDownTimer() {
    104         if (mCountDownTimer != null) {
    105             mCountDownTimer.cancel();
    106         }
    107     }
    108 }
    109