• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.server.telecom.testapps;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 import android.os.Handler;
22 import android.telecom.Call;
23 import android.util.Log;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.BaseAdapter;
28 import android.widget.TextView;
29 
30 public class CallListAdapter extends BaseAdapter {
31     private static final String TAG = "CallListAdapter";
32 
33     private final TestCallList.Listener mListener = new TestCallList.Listener() {
34         @Override
35         public void onCallAdded(Call call) {
36             notifyDataSetChanged();
37         }
38 
39         @Override
40         public void onCallRemoved(Call call) {
41             notifyDataSetChanged();
42             if (mCallList.size() == 0) {
43                 mCallList.removeListener(this);
44             }
45         }
46     };
47 
48     private final LayoutInflater mLayoutInflater;
49     private final TestCallList mCallList;
50     private final Handler mHandler = new Handler();
51     private final Runnable mSecondsRunnable = new Runnable() {
52         @Override
53         public void run() {
54             notifyDataSetChanged();
55             if (mCallList.size() > 0) {
56                 mHandler.postDelayed(this, 1000);
57             }
58         }
59     };
60 
CallListAdapter(Context context)61     public CallListAdapter(Context context) {
62         mLayoutInflater =
63                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
64         mCallList = TestCallList.getInstance();
65         mCallList.addListener(mListener);
66         mHandler.postDelayed(mSecondsRunnable, 1000);
67     }
68 
69 
70     @Override
getCount()71     public int getCount() {
72         Log.i(TAG, "size reporting: " + mCallList.size());
73         return mCallList.size();
74     }
75 
76     @Override
getItem(int position)77     public Object getItem(int position) {
78         return position;
79     }
80 
81     @Override
getItemId(int position)82     public long getItemId(int position) {
83         return position;
84     }
85 
86     @Override
getView(final int position, View convertView, ViewGroup parent)87     public View getView(final int position, View convertView, ViewGroup parent) {
88         Log.i(TAG, "getView: " + position);
89         if (convertView == null) {
90             convertView = mLayoutInflater.inflate(R.layout.call_list_item, parent, false);
91         }
92 
93         TextView phoneNumber = (TextView) convertView.findViewById(R.id.phoneNumber);
94         TextView duration = (TextView) convertView.findViewById(R.id.duration);
95         TextView state = (TextView) convertView.findViewById(R.id.callState);
96 
97         Call call = mCallList.getCall(position);
98         Uri handle = call.getDetails().getHandle();
99         phoneNumber.setText(handle == null ? "No number" : handle.getSchemeSpecificPart());
100 
101         long durationMs = System.currentTimeMillis() - call.getDetails().getConnectTimeMillis();
102         duration.setText((durationMs / 1000) + " secs");
103 
104         state.setText(getStateString(call));
105 
106         Log.i(TAG, "Call found: " + ((handle == null) ? "null" : handle.getSchemeSpecificPart())
107                 + ", " + durationMs);
108 
109         return convertView;
110     }
111 
getStateString(Call call)112     private static String getStateString(Call call) {
113         switch (call.getState()) {
114             case Call.STATE_ACTIVE:
115                 return "active";
116             case Call.STATE_CONNECTING:
117                 return "connecting";
118             case Call.STATE_DIALING:
119                 return "dialing";
120             case Call.STATE_DISCONNECTED:
121                 return "disconnected";
122             case Call.STATE_DISCONNECTING:
123                 return "disconnecting";
124             case Call.STATE_HOLDING:
125                 return "on hold";
126             case Call.STATE_NEW:
127                 return "new";
128             case Call.STATE_RINGING:
129                 return "ringing";
130             case Call.STATE_SELECT_PHONE_ACCOUNT:
131                 return "select phone account";
132             default:
133                 return "unknown";
134         }
135     }
136 }
137