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.app.ActionBar.Tab; 23 import android.app.ActionBar.TabListener; 24 import android.app.FragmentTransaction; 25 import android.content.Intent; 26 import android.os.Bundle; 27 import android.provider.CallLog.Calls; 28 import android.support.v13.app.FragmentPagerAdapter; 29 import android.support.v4.view.PagerTabStrip; 30 import android.support.v4.view.ViewPager; 31 import android.support.v4.view.ViewPager.OnPageChangeListener; 32 import android.text.Spannable; 33 import android.text.SpannableString; 34 import android.text.style.TypefaceSpan; 35 import android.view.Menu; 36 import android.view.MenuInflater; 37 import android.view.MenuItem; 38 39 import com.android.dialer.DialtactsActivity; 40 import com.android.dialer.R; 41 import com.android.dialer.calllog.CallLogFragment; 42 43 public class CallLogActivity extends Activity { 44 45 private ViewPager mViewPager; 46 private ViewPagerAdapter mViewPagerAdapter; 47 private CallLogFragment mAllCallsFragment; 48 private CallLogFragment mMissedCallsFragment; 49 50 private static final int TAB_INDEX_ALL = 0; 51 private static final int TAB_INDEX_MISSED = 1; 52 53 private static final int TAB_INDEX_COUNT = 2; 54 55 public class ViewPagerAdapter extends FragmentPagerAdapter { ViewPagerAdapter(FragmentManager fm)56 public ViewPagerAdapter(FragmentManager fm) { 57 super(fm); 58 } 59 60 @Override getItem(int position)61 public Fragment getItem(int position) { 62 switch (position) { 63 case TAB_INDEX_ALL: 64 mAllCallsFragment = new CallLogFragment(CallLogQueryHandler.CALL_TYPE_ALL); 65 return mAllCallsFragment; 66 case TAB_INDEX_MISSED: 67 mMissedCallsFragment = new CallLogFragment(Calls.MISSED_TYPE); 68 return mMissedCallsFragment; 69 } 70 throw new IllegalStateException("No fragment at position " + position); 71 } 72 73 @Override getCount()74 public int getCount() { 75 return TAB_INDEX_COUNT; 76 } 77 } 78 79 private final TabListener mTabListener = new TabListener() { 80 @Override 81 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 82 } 83 84 @Override 85 public void onTabSelected(Tab tab, FragmentTransaction ft) { 86 if (mViewPager != null && mViewPager.getCurrentItem() != tab.getPosition()) { 87 mViewPager.setCurrentItem(tab.getPosition(), true); 88 } 89 } 90 91 @Override 92 public void onTabReselected(Tab tab, FragmentTransaction ft) { 93 } 94 }; 95 96 private final OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() { 97 98 @Override 99 public void onPageScrolled( 100 int position, float positionOffset, int positionOffsetPixels) {} 101 102 @Override 103 public void onPageSelected(int position) { 104 final ActionBar actionBar = getActionBar(); 105 actionBar.selectTab(actionBar.getTabAt(position)); 106 } 107 108 @Override 109 public void onPageScrollStateChanged(int arg0) { 110 } 111 }; 112 113 @Override onCreate(Bundle savedInstanceState)114 protected void onCreate(Bundle savedInstanceState) { 115 super.onCreate(savedInstanceState); 116 117 setContentView(R.layout.call_log_activity); 118 119 final ActionBar actionBar = getActionBar(); 120 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 121 actionBar.setDisplayShowHomeEnabled(true); 122 actionBar.setDisplayHomeAsUpEnabled(true); 123 actionBar.setDisplayShowTitleEnabled(true); 124 125 final Tab allTab = actionBar.newTab(); 126 final String allTitle = getString(R.string.call_log_all_title); 127 allTab.setContentDescription(allTitle); 128 allTab.setText(allTitle); 129 allTab.setTabListener(mTabListener); 130 actionBar.addTab(allTab); 131 132 final Tab missedTab = actionBar.newTab(); 133 final String missedTitle = getString(R.string.call_log_missed_title); 134 missedTab.setContentDescription(missedTitle); 135 missedTab.setText(missedTitle); 136 missedTab.setTabListener(mTabListener); 137 actionBar.addTab(missedTab); 138 139 mViewPager = (ViewPager) findViewById(R.id.call_log_pager); 140 mViewPagerAdapter = new ViewPagerAdapter(getFragmentManager()); 141 mViewPager.setAdapter(mViewPagerAdapter); 142 mViewPager.setOnPageChangeListener(mOnPageChangeListener); 143 mViewPager.setOffscreenPageLimit(1); 144 } 145 146 @Override onCreateOptionsMenu(Menu menu)147 public boolean onCreateOptionsMenu(Menu menu) { 148 final MenuInflater inflater = getMenuInflater(); 149 inflater.inflate(R.menu.call_log_options, menu); 150 return true; 151 } 152 153 @Override onPrepareOptionsMenu(Menu menu)154 public boolean onPrepareOptionsMenu(Menu menu) { 155 final MenuItem itemDeleteAll = menu.findItem(R.id.delete_all); 156 157 // If onPrepareOptionsMenu is called before fragments loaded. Don't do anything. 158 if (mAllCallsFragment != null && itemDeleteAll != null) { 159 final CallLogAdapter adapter = mAllCallsFragment.getAdapter(); 160 itemDeleteAll.setVisible(adapter != null && !adapter.isEmpty()); 161 } 162 return true; 163 } 164 165 @Override onOptionsItemSelected(MenuItem item)166 public boolean onOptionsItemSelected(MenuItem item) { 167 switch (item.getItemId()) { 168 case android.R.id.home: 169 final Intent intent = new Intent(this, DialtactsActivity.class); 170 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 171 startActivity(intent); 172 return true; 173 case R.id.delete_all: 174 ClearCallLogDialog.show(getFragmentManager()); 175 return true; 176 } 177 return super.onOptionsItemSelected(item); 178 } 179 } 180