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 17package com.android.server.telecom.testapps; 18 19import android.content.Context; 20import android.net.Uri; 21import android.os.Handler; 22import android.telecom.Call; 23import android.util.Log; 24import android.view.LayoutInflater; 25import android.view.View; 26import android.view.ViewGroup; 27import android.widget.BaseAdapter; 28import android.widget.TextView; 29 30public 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 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 71 public int getCount() { 72 Log.i(TAG, "size reporting: " + mCallList.size()); 73 return mCallList.size(); 74 } 75 76 @Override 77 public Object getItem(int position) { 78 return position; 79 } 80 81 @Override 82 public long getItemId(int position) { 83 return position; 84 } 85 86 @Override 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 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