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