1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dialer.calllog;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.android.contacts.common.list.ViewPagerTabs;
import com.android.dialer.DialtactsActivity;
import com.android.dialer.R;
import com.android.dialer.voicemail.VoicemailStatusHelper;
import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
import com.android.dialerbind.analytics.AnalyticsActivity;
public class CallLogActivity extends AnalyticsActivity implements CallLogQueryHandler.Listener {
private Handler mHandler;
private ViewPager mViewPager;
private ViewPagerTabs mViewPagerTabs;
private ViewPagerAdapter mViewPagerAdapter;
private CallLogFragment mAllCallsFragment;
private CallLogFragment mMissedCallsFragment;
private CallLogFragment mVoicemailFragment;
private VoicemailStatusHelper mVoicemailStatusHelper;
private static final int WAIT_FOR_VOICEMAIL_PROVIDER_TIMEOUT_MS = 300;
private boolean mSwitchToVoicemailTab;
private String[] mTabTitles;
private static final int TAB_INDEX_ALL = 0;
private static final int TAB_INDEX_MISSED = 1;
private static final int TAB_INDEX_VOICEMAIL = 2;
private static final int TAB_INDEX_COUNT_DEFAULT = 2;
private static final int TAB_INDEX_COUNT_WITH_VOICEMAIL = 3;
private boolean mHasActiveVoicemailProvider;
private final Runnable mWaitForVoicemailTimeoutRunnable = new Runnable() {
@Override
public void run() {
mViewPagerTabs.setViewPager(mViewPager);
mViewPager.setCurrentItem(TAB_INDEX_ALL);
mSwitchToVoicemailTab = false;
}
};
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case TAB_INDEX_ALL:
mAllCallsFragment = new CallLogFragment(CallLogQueryHandler.CALL_TYPE_ALL);
return mAllCallsFragment;
case TAB_INDEX_MISSED:
mMissedCallsFragment = new CallLogFragment(Calls.MISSED_TYPE);
return mMissedCallsFragment;
case TAB_INDEX_VOICEMAIL:
mVoicemailFragment = new CallLogFragment(Calls.VOICEMAIL_TYPE);
return mVoicemailFragment;
}
throw new IllegalStateException("No fragment at position " + position);
}
@Override
public CharSequence getPageTitle(int position) {
return mTabTitles[position];
}
@Override
public int getCount() {
return mHasActiveVoicemailProvider ? TAB_INDEX_COUNT_WITH_VOICEMAIL :
TAB_INDEX_COUNT_DEFAULT;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
setContentView(R.layout.call_log_activity);
getWindow().setBackgroundDrawable(null);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
int startingTab = TAB_INDEX_ALL;
final Intent intent = getIntent();
if (intent != null) {
final int callType = intent.getIntExtra(CallLog.Calls.EXTRA_CALL_TYPE_FILTER, -1);
if (callType == CallLog.Calls.MISSED_TYPE) {
startingTab = TAB_INDEX_MISSED;
} else if (callType == CallLog.Calls.VOICEMAIL_TYPE) {
startingTab = TAB_INDEX_VOICEMAIL;
}
}
mTabTitles = new String[TAB_INDEX_COUNT_WITH_VOICEMAIL];
mTabTitles[0] = getString(R.string.call_log_all_title);
mTabTitles[1] = getString(R.string.call_log_missed_title);
mTabTitles[2] = getString(R.string.call_log_voicemail_title);
mViewPager = (ViewPager) findViewById(R.id.call_log_pager);
mViewPagerAdapter = new ViewPagerAdapter(getFragmentManager());
mViewPager.setAdapter(mViewPagerAdapter);
mViewPager.setOffscreenPageLimit(2);
mViewPagerTabs = (ViewPagerTabs) findViewById(R.id.viewpager_header);
mViewPager.setOnPageChangeListener(mViewPagerTabs);
if (startingTab == TAB_INDEX_VOICEMAIL) {
// The addition of the voicemail tab is an asynchronous process, so wait till the tab
// is added, before attempting to switch to it. If the querying of CP2 for voicemail
// providers takes too long, give up and show the first tab instead.
mSwitchToVoicemailTab = true;
mHandler.postDelayed(mWaitForVoicemailTimeoutRunnable,
WAIT_FOR_VOICEMAIL_PROVIDER_TIMEOUT_MS);
} else {
mViewPagerTabs.setViewPager(mViewPager);
mViewPager.setCurrentItem(startingTab);
}
mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
}
@Override
protected void onResume() {
super.onResume();
CallLogQueryHandler callLogQueryHandler =
new CallLogQueryHandler(this.getContentResolver(), this);
callLogQueryHandler.fetchVoicemailStatus();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.call_log_options, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final MenuItem itemDeleteAll = menu.findItem(R.id.delete_all);
// If onPrepareOptionsMenu is called before fragments loaded. Don't do anything.
if (mAllCallsFragment != null && itemDeleteAll != null) {
final CallLogAdapter adapter = mAllCallsFragment.getAdapter();
itemDeleteAll.setVisible(adapter != null && !adapter.isEmpty());
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
final Intent intent = new Intent(this, DialtactsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.delete_all:
ClearCallLogDialog.show(getFragmentManager());
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onVoicemailStatusFetched(Cursor statusCursor) {
if (this.isFinishing()) {
return;
}
mHandler.removeCallbacks(mWaitForVoicemailTimeoutRunnable);
// Update mHasActiveVoicemailProvider, which controls the number of tabs displayed.
int activeSources = mVoicemailStatusHelper.getNumberActivityVoicemailSources(statusCursor);
if (activeSources > 0 != mHasActiveVoicemailProvider) {
mHasActiveVoicemailProvider = activeSources > 0;
mViewPagerAdapter.notifyDataSetChanged();
mViewPagerTabs.setViewPager(mViewPager);
if (mSwitchToVoicemailTab) {
mViewPager.setCurrentItem(TAB_INDEX_VOICEMAIL, false);
}
} else if (mSwitchToVoicemailTab) {
// The voicemail tab was requested, but it does not exist because there are no
// voicemail sources. Just fallback to the first item instead.
mViewPagerTabs.setViewPager(mViewPager);
}
}
@Override
public void onCallsFetched(Cursor statusCursor) {
// Do nothing. Implemented to satisfy CallLogQueryHandler.Listener.
}
}
|