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 package com.android.dialer.calllog; 17 18 import android.app.ActionBar; 19 import android.app.Activity; 20 import android.app.Fragment; 21 import android.app.FragmentManager; 22 import android.content.Intent; 23 import android.database.Cursor; 24 import android.os.Bundle; 25 import android.os.Handler; 26 import android.provider.CallLog; 27 import android.provider.CallLog.Calls; 28 import android.support.v13.app.FragmentPagerAdapter; 29 import android.support.v4.view.ViewPager; 30 import android.view.Menu; 31 import android.view.MenuInflater; 32 import android.view.MenuItem; 33 import android.view.MotionEvent; 34 import android.view.ViewGroup; 35 36 import com.android.contacts.common.interactions.TouchPointManager; 37 import com.android.contacts.common.list.ViewPagerTabs; 38 import com.android.contacts.common.util.PermissionsUtil; 39 import com.android.contacts.commonbind.analytics.AnalyticsUtil; 40 import com.android.dialer.DialtactsActivity; 41 import com.android.dialer.R; 42 import com.android.dialer.util.DialerUtils; 43 import com.android.dialer.voicemail.VoicemailStatusHelper; 44 import com.android.dialer.voicemail.VoicemailStatusHelperImpl; 45 46 public class CallLogActivity extends Activity implements ViewPager.OnPageChangeListener { 47 private ViewPager mViewPager; 48 private ViewPagerTabs mViewPagerTabs; 49 private ViewPagerAdapter mViewPagerAdapter; 50 private CallLogFragment mAllCallsFragment; 51 private CallLogFragment mMissedCallsFragment; 52 53 private String[] mTabTitles; 54 55 private static final int TAB_INDEX_ALL = 0; 56 private static final int TAB_INDEX_MISSED = 1; 57 58 private static final int TAB_INDEX_COUNT = 2; 59 60 private boolean mIsResumed; 61 62 public class ViewPagerAdapter extends FragmentPagerAdapter { ViewPagerAdapter(FragmentManager fm)63 public ViewPagerAdapter(FragmentManager fm) { 64 super(fm); 65 } 66 67 @Override getItemId(int position)68 public long getItemId(int position) { 69 return getRtlPosition(position); 70 } 71 72 @Override getItem(int position)73 public Fragment getItem(int position) { 74 switch (getRtlPosition(position)) { 75 case TAB_INDEX_ALL: 76 return new CallLogFragment(CallLogQueryHandler.CALL_TYPE_ALL); 77 case TAB_INDEX_MISSED: 78 return new CallLogFragment(Calls.MISSED_TYPE); 79 } 80 throw new IllegalStateException("No fragment at position " + position); 81 } 82 83 @Override instantiateItem(ViewGroup container, int position)84 public Object instantiateItem(ViewGroup container, int position) { 85 final CallLogFragment fragment = 86 (CallLogFragment) super.instantiateItem(container, position); 87 switch (position) { 88 case TAB_INDEX_ALL: 89 mAllCallsFragment = fragment; 90 break; 91 case TAB_INDEX_MISSED: 92 mMissedCallsFragment = fragment; 93 break; 94 } 95 return fragment; 96 } 97 98 @Override getPageTitle(int position)99 public CharSequence getPageTitle(int position) { 100 return mTabTitles[position]; 101 } 102 103 @Override getCount()104 public int getCount() { 105 return TAB_INDEX_COUNT; 106 } 107 } 108 109 @Override dispatchTouchEvent(MotionEvent ev)110 public boolean dispatchTouchEvent(MotionEvent ev) { 111 if (ev.getAction() == MotionEvent.ACTION_DOWN) { 112 TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY()); 113 } 114 return super.dispatchTouchEvent(ev); 115 } 116 117 @Override onCreate(Bundle savedInstanceState)118 protected void onCreate(Bundle savedInstanceState) { 119 super.onCreate(savedInstanceState); 120 121 setContentView(R.layout.call_log_activity); 122 getWindow().setBackgroundDrawable(null); 123 124 final ActionBar actionBar = getActionBar(); 125 actionBar.setDisplayShowHomeEnabled(true); 126 actionBar.setDisplayHomeAsUpEnabled(true); 127 actionBar.setDisplayShowTitleEnabled(true); 128 actionBar.setElevation(0); 129 130 int startingTab = TAB_INDEX_ALL; 131 final Intent intent = getIntent(); 132 if (intent != null) { 133 final int callType = intent.getIntExtra(CallLog.Calls.EXTRA_CALL_TYPE_FILTER, -1); 134 if (callType == CallLog.Calls.MISSED_TYPE) { 135 startingTab = TAB_INDEX_MISSED; 136 } 137 } 138 139 mTabTitles = new String[TAB_INDEX_COUNT]; 140 mTabTitles[0] = getString(R.string.call_log_all_title); 141 mTabTitles[1] = getString(R.string.call_log_missed_title); 142 143 mViewPager = (ViewPager) findViewById(R.id.call_log_pager); 144 145 mViewPagerAdapter = new ViewPagerAdapter(getFragmentManager()); 146 mViewPager.setAdapter(mViewPagerAdapter); 147 mViewPager.setOffscreenPageLimit(1); 148 mViewPager.setOnPageChangeListener(this); 149 150 mViewPagerTabs = (ViewPagerTabs) findViewById(R.id.viewpager_header); 151 152 mViewPagerTabs.setViewPager(mViewPager); 153 mViewPager.setCurrentItem(startingTab); 154 } 155 156 @Override onResume()157 protected void onResume() { 158 mIsResumed = true; 159 super.onResume(); 160 sendScreenViewForChildFragment(mViewPager.getCurrentItem()); 161 } 162 163 @Override onPause()164 protected void onPause() { 165 mIsResumed = false; 166 super.onPause(); 167 } 168 169 @Override onCreateOptionsMenu(Menu menu)170 public boolean onCreateOptionsMenu(Menu menu) { 171 final MenuInflater inflater = getMenuInflater(); 172 inflater.inflate(R.menu.call_log_options, menu); 173 return true; 174 } 175 176 @Override onPrepareOptionsMenu(Menu menu)177 public boolean onPrepareOptionsMenu(Menu menu) { 178 final MenuItem itemDeleteAll = menu.findItem(R.id.delete_all); 179 if (mAllCallsFragment != null && itemDeleteAll != null) { 180 // If onPrepareOptionsMenu is called before fragments are loaded, don't do anything. 181 final CallLogAdapter adapter = mAllCallsFragment.getAdapter(); 182 itemDeleteAll.setVisible(adapter != null && !adapter.isEmpty()); 183 } 184 return true; 185 } 186 187 @Override onOptionsItemSelected(MenuItem item)188 public boolean onOptionsItemSelected(MenuItem item) { 189 switch (item.getItemId()) { 190 case android.R.id.home: 191 final Intent intent = new Intent(this, DialtactsActivity.class); 192 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 193 startActivity(intent); 194 return true; 195 case R.id.delete_all: 196 ClearCallLogDialog.show(getFragmentManager()); 197 return true; 198 } 199 return super.onOptionsItemSelected(item); 200 } 201 202 @Override onPageScrolled(int position, float positionOffset, int positionOffsetPixels)203 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 204 mViewPagerTabs.onPageScrolled(position, positionOffset, positionOffsetPixels); 205 } 206 207 @Override onPageSelected(int position)208 public void onPageSelected(int position) { 209 if (mIsResumed) { 210 sendScreenViewForChildFragment(position); 211 } 212 mViewPagerTabs.onPageSelected(position); 213 } 214 215 @Override onPageScrollStateChanged(int state)216 public void onPageScrollStateChanged(int state) { 217 mViewPagerTabs.onPageScrollStateChanged(state); 218 } 219 sendScreenViewForChildFragment(int position)220 private void sendScreenViewForChildFragment(int position) { 221 AnalyticsUtil.sendScreenView(CallLogFragment.class.getSimpleName(), this, 222 getFragmentTagForPosition(position)); 223 } 224 225 /** 226 * Returns the fragment located at the given position in the {@link ViewPagerAdapter}. May 227 * be null if the position is invalid. 228 */ getFragmentTagForPosition(int position)229 private String getFragmentTagForPosition(int position) { 230 switch (position) { 231 case TAB_INDEX_ALL: 232 return "All"; 233 case TAB_INDEX_MISSED: 234 return "Missed"; 235 } 236 return null; 237 } 238 getRtlPosition(int position)239 private int getRtlPosition(int position) { 240 if (DialerUtils.isRtl()) { 241 return mViewPagerAdapter.getCount() - 1 - position; 242 } 243 return position; 244 } 245 } 246