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
17package com.android.incallui;
18
19import android.content.Context;
20import android.os.Handler;
21import android.os.Message;
22import android.telecom.VideoProfile;
23import android.util.AttributeSet;
24import android.view.View;
25
26import com.android.incallui.widget.multiwaveview.GlowPadView;
27
28/**
29 *
30 */
31public 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    private int mVideoState = VideoProfile.STATE_BIDIRECTIONAL;
53
54    public GlowPadWrapper(Context context) {
55        super(context);
56        Log.d(this, "class created " + this + " ");
57    }
58
59    public GlowPadWrapper(Context context, AttributeSet attrs) {
60        super(context, attrs);
61        Log.d(this, "class created " + this);
62    }
63
64    @Override
65    protected void onFinishInflate() {
66        Log.d(this, "onFinishInflate()");
67        super.onFinishInflate();
68        setOnTriggerListener(this);
69    }
70
71    public void startPing() {
72        Log.d(this, "startPing");
73        mPingEnabled = true;
74        triggerPing();
75    }
76
77    public void stopPing() {
78        Log.d(this, "stopPing");
79        mPingEnabled = false;
80        mPingHandler.removeMessages(PING_MESSAGE_WHAT);
81    }
82
83    private void triggerPing() {
84        Log.d(this, "triggerPing(): " + mPingEnabled + " " + this);
85        if (mPingEnabled && !mPingHandler.hasMessages(PING_MESSAGE_WHAT)) {
86            ping();
87
88            if (ENABLE_PING_AUTO_REPEAT) {
89                mPingHandler.sendEmptyMessageDelayed(PING_MESSAGE_WHAT, PING_REPEAT_DELAY_MS);
90            }
91        }
92    }
93
94    @Override
95    public void onGrabbed(View v, int handle) {
96        Log.d(this, "onGrabbed()");
97        stopPing();
98    }
99
100    @Override
101    public void onReleased(View v, int handle) {
102        Log.d(this, "onReleased()");
103        if (mTargetTriggered) {
104            mTargetTriggered = false;
105        } else {
106            startPing();
107        }
108    }
109
110    @Override
111    public void onTrigger(View v, int target) {
112        Log.d(this, "onTrigger() view=" + v + " target=" + target);
113        final int resId = getResourceIdForTarget(target);
114        switch (resId) {
115            case R.drawable.ic_lockscreen_answer:
116                mAnswerListener.onAnswer(VideoProfile.STATE_AUDIO_ONLY, getContext());
117                mTargetTriggered = true;
118                break;
119            case R.drawable.ic_lockscreen_decline:
120                mAnswerListener.onDecline(getContext());
121                mTargetTriggered = true;
122                break;
123            case R.drawable.ic_lockscreen_text:
124                mAnswerListener.onText();
125                mTargetTriggered = true;
126                break;
127            case R.drawable.ic_videocam:
128            case R.drawable.ic_lockscreen_answer_video:
129                mAnswerListener.onAnswer(mVideoState, getContext());
130                mTargetTriggered = true;
131                break;
132            case R.drawable.ic_lockscreen_decline_video:
133                mAnswerListener.onDeclineUpgradeRequest(getContext());
134                mTargetTriggered = true;
135                break;
136            default:
137                // Code should never reach here.
138                Log.e(this, "Trigger detected on unhandled resource. Skipping.");
139        }
140    }
141
142    @Override
143    public void onGrabbedStateChange(View v, int handle) {
144
145    }
146
147    @Override
148    public void onFinishFinalAnimation() {
149
150    }
151
152    public void setAnswerListener(AnswerListener listener) {
153        mAnswerListener = listener;
154    }
155
156    /**
157     * Sets the video state represented by the "video" icon on the glow pad.
158     *
159     * @param videoState The new video state.
160     */
161    public void setVideoState(int videoState) {
162        mVideoState = videoState;
163    }
164
165    public interface AnswerListener {
166        void onAnswer(int videoState, Context context);
167        void onDecline(Context context);
168        void onDeclineUpgradeRequest(Context context);
169        void onText();
170    }
171}
172