1 /*
2  * Copyright (C) 2013 The Android Open Source Project
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.android.incallui;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Message;
22 import android.telecom.VideoProfile;
23 import android.util.AttributeSet;
24 import android.view.View;
25 
26 import com.android.incallui.widget.multiwaveview.GlowPadView;
27 
28 /**
29  *
30  */
31 public class GlowPadWrapper extends GlowPadView implements GlowPadView.OnTriggerListener {
32 
33     // Parameters for the GlowPadView "ping" animation; see triggerPing().
34     private static final int PING_MESSAGE_WHAT = 101;
35     private static final boolean ENABLE_PING_AUTO_REPEAT = true;
36     private static final long PING_REPEAT_DELAY_MS = 1200;
37 
38     private final Handler mPingHandler = new Handler() {
39         @Override
40         public void handleMessage(Message msg) {
41             switch (msg.what) {
42                 case PING_MESSAGE_WHAT:
43                     triggerPing();
44                     break;
45             }
46         }
47     };
48 
49     private AnswerListener mAnswerListener;
50     private boolean mPingEnabled = true;
51     private boolean mTargetTriggered = false;
52 
GlowPadWrapper(Context context)53     public GlowPadWrapper(Context context) {
54         super(context);
55         Log.d(this, "class created " + this + " ");
56     }
57 
GlowPadWrapper(Context context, AttributeSet attrs)58     public GlowPadWrapper(Context context, AttributeSet attrs) {
59         super(context, attrs);
60         Log.d(this, "class created " + this);
61     }
62 
63     @Override
onFinishInflate()64     protected void onFinishInflate() {
65         Log.d(this, "onFinishInflate()");
66         super.onFinishInflate();
67         setOnTriggerListener(this);
68     }
69 
startPing()70     public void startPing() {
71         Log.d(this, "startPing");
72         mPingEnabled = true;
73         triggerPing();
74     }
75 
stopPing()76     public void stopPing() {
77         Log.d(this, "stopPing");
78         mPingEnabled = false;
79         mPingHandler.removeMessages(PING_MESSAGE_WHAT);
80     }
81 
triggerPing()82     private void triggerPing() {
83         Log.d(this, "triggerPing(): " + mPingEnabled + " " + this);
84         if (mPingEnabled && !mPingHandler.hasMessages(PING_MESSAGE_WHAT)) {
85             ping();
86 
87             if (ENABLE_PING_AUTO_REPEAT) {
88                 mPingHandler.sendEmptyMessageDelayed(PING_MESSAGE_WHAT, PING_REPEAT_DELAY_MS);
89             }
90         }
91     }
92 
93     @Override
onGrabbed(View v, int handle)94     public void onGrabbed(View v, int handle) {
95         Log.d(this, "onGrabbed()");
96         stopPing();
97     }
98 
99     @Override
onReleased(View v, int handle)100     public void onReleased(View v, int handle) {
101         Log.d(this, "onReleased()");
102         if (mTargetTriggered) {
103             mTargetTriggered = false;
104         } else {
105             startPing();
106         }
107     }
108 
109     @Override
onTrigger(View v, int target)110     public void onTrigger(View v, int target) {
111         Log.d(this, "onTrigger()");
112         final int resId = getResourceIdForTarget(target);
113         switch (resId) {
114             case R.drawable.ic_lockscreen_answer:
115                 mAnswerListener.onAnswer(VideoProfile.VideoState.AUDIO_ONLY, getContext());
116                 mTargetTriggered = true;
117                 break;
118             case R.drawable.ic_lockscreen_decline:
119                 mAnswerListener.onDecline();
120                 mTargetTriggered = true;
121                 break;
122             case R.drawable.ic_lockscreen_text:
123                 mAnswerListener.onText();
124                 mTargetTriggered = true;
125                 break;
126             case R.drawable.ic_videocam:
127                 mAnswerListener.onAnswer(VideoProfile.VideoState.BIDIRECTIONAL, getContext());
128                 mTargetTriggered = true;
129                 break;
130             case R.drawable.ic_toolbar_video_off:
131                 InCallPresenter.getInstance().declineUpgradeRequest(getContext());
132                 mTargetTriggered = true;
133                 break;
134             default:
135                 // Code should never reach here.
136                 Log.e(this, "Trigger detected on unhandled resource. Skipping.");
137         }
138     }
139 
140     @Override
onGrabbedStateChange(View v, int handle)141     public void onGrabbedStateChange(View v, int handle) {
142 
143     }
144 
145     @Override
onFinishFinalAnimation()146     public void onFinishFinalAnimation() {
147 
148     }
149 
setAnswerListener(AnswerListener listener)150     public void setAnswerListener(AnswerListener listener) {
151         mAnswerListener = listener;
152     }
153 
154     public interface AnswerListener {
onAnswer(int videoState, Context context)155         void onAnswer(int videoState, Context context);
onDecline()156         void onDecline();
onText()157         void onText();
158     }
159 }
160