1 /*
2  * Copyright (C) 2017 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 
17 package com.android.dialer.main.impl;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.support.v4.content.LocalBroadcastManager;
23 import com.android.dialer.blockreportspam.ShowBlockReportSpamDialogReceiver;
24 import com.android.dialer.calllog.config.CallLogConfigComponent;
25 import com.android.dialer.common.Assert;
26 import com.android.dialer.common.LogUtil;
27 import com.android.dialer.interactions.PhoneNumberInteraction.DisambigDialogDismissedListener;
28 import com.android.dialer.interactions.PhoneNumberInteraction.InteractionErrorCode;
29 import com.android.dialer.interactions.PhoneNumberInteraction.InteractionErrorListener;
30 import com.android.dialer.main.MainActivityPeer;
31 import com.android.dialer.main.impl.bottomnav.BottomNavBar.TabIndex;
32 import com.android.dialer.util.TransactionSafeActivity;
33 
34 /** This is the main activity for dialer. It hosts favorites, call log, search, dialpad, etc... */
35 // TODO(calderwoodra): Do not extend TransactionSafeActivity after new SpeedDial is launched
36 public class MainActivity extends TransactionSafeActivity
37     implements MainActivityPeer.PeerSupplier,
38         // TODO(calderwoodra): remove these 2 interfaces when we migrate to new speed dial fragment
39         InteractionErrorListener,
40         DisambigDialogDismissedListener {
41 
42   private MainActivityPeer activePeer;
43 
44   /**
45    * {@link android.content.BroadcastReceiver} that shows a dialog to block a number and/or report
46    * it as spam when notified.
47    */
48   private ShowBlockReportSpamDialogReceiver showBlockReportSpamDialogReceiver;
49 
getShowCallLogIntent(Context context)50   public static Intent getShowCallLogIntent(Context context) {
51     return getShowTabIntent(context, TabIndex.CALL_LOG);
52   }
53 
54   /** Returns intent that will open MainActivity to the specified tab. */
getShowTabIntent(Context context, @TabIndex int tabIndex)55   public static Intent getShowTabIntent(Context context, @TabIndex int tabIndex) {
56     if (CallLogConfigComponent.get(context).callLogConfig().isNewPeerEnabled()) {
57       // TODO(calderwoodra): implement this in NewMainActivityPeer
58       return null;
59     }
60     return OldMainActivityPeer.getShowTabIntent(context, tabIndex);
61   }
62 
63   /**
64    * @param context Context of the application package implementing MainActivity class.
65    * @return intent for MainActivity.class
66    */
getIntent(Context context)67   public static Intent getIntent(Context context) {
68     return new Intent(context, MainActivity.class)
69         .setAction(Intent.ACTION_VIEW)
70         .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
71   }
72 
73   @Override
onCreate(Bundle savedInstanceState)74   protected void onCreate(Bundle savedInstanceState) {
75     super.onCreate(savedInstanceState);
76     LogUtil.enterBlock("MainActivity.onCreate");
77     // If peer was set by the super, don't reset it.
78     activePeer = getNewPeer();
79     activePeer.onActivityCreate(savedInstanceState);
80 
81     showBlockReportSpamDialogReceiver =
82         new ShowBlockReportSpamDialogReceiver(getSupportFragmentManager());
83   }
84 
getNewPeer()85   protected MainActivityPeer getNewPeer() {
86     if (CallLogConfigComponent.get(this).callLogConfig().isNewPeerEnabled()) {
87       return new NewMainActivityPeer(this);
88     } else {
89       return new OldMainActivityPeer(this);
90     }
91   }
92 
93   @Override
onNewIntent(Intent intent)94   protected void onNewIntent(Intent intent) {
95     super.onNewIntent(intent);
96     setIntent(intent);
97     activePeer.onNewIntent(intent);
98   }
99 
100   @Override
onResume()101   protected void onResume() {
102     super.onResume();
103     activePeer.onActivityResume();
104 
105     LocalBroadcastManager.getInstance(this)
106         .registerReceiver(
107             showBlockReportSpamDialogReceiver, ShowBlockReportSpamDialogReceiver.getIntentFilter());
108   }
109 
110   @Override
onUserLeaveHint()111   protected void onUserLeaveHint() {
112     super.onUserLeaveHint();
113     activePeer.onUserLeaveHint();
114   }
115 
116   @Override
onPause()117   protected void onPause() {
118     super.onPause();
119     activePeer.onActivityPause();
120 
121     LocalBroadcastManager.getInstance(this).unregisterReceiver(showBlockReportSpamDialogReceiver);
122   }
123 
124   @Override
onStop()125   protected void onStop() {
126     super.onStop();
127     activePeer.onActivityStop();
128   }
129 
130   @Override
onSaveInstanceState(Bundle bundle)131   protected void onSaveInstanceState(Bundle bundle) {
132     super.onSaveInstanceState(bundle);
133     activePeer.onSaveInstanceState(bundle);
134   }
135 
136   @Override
onActivityResult(int requestCode, int resultCode, Intent data)137   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
138     super.onActivityResult(requestCode, resultCode, data);
139     activePeer.onActivityResult(requestCode, resultCode, data);
140   }
141 
142   @Override
onBackPressed()143   public void onBackPressed() {
144     if (activePeer.onBackPressed()) {
145       return;
146     }
147     super.onBackPressed();
148   }
149 
150   @Override
interactionError(@nteractionErrorCode int interactionErrorCode)151   public void interactionError(@InteractionErrorCode int interactionErrorCode) {
152     switch (interactionErrorCode) {
153       case InteractionErrorCode.USER_LEAVING_ACTIVITY:
154         // This is expected to happen if the user exits the activity before the interaction occurs.
155         return;
156       case InteractionErrorCode.CONTACT_NOT_FOUND:
157       case InteractionErrorCode.CONTACT_HAS_NO_NUMBER:
158       case InteractionErrorCode.OTHER_ERROR:
159       default:
160         // All other error codes are unexpected. For example, it should be impossible to start an
161         // interaction with an invalid contact from this activity.
162         throw Assert.createIllegalStateFailException(
163             "PhoneNumberInteraction error: " + interactionErrorCode);
164     }
165   }
166 
167   @Override
onDisambigDialogDismissed()168   public void onDisambigDialogDismissed() {
169     // Don't do anything; the app will remain open with favorites tiles displayed.
170   }
171 
172   @Override
getPeer()173   public MainActivityPeer getPeer() {
174     return activePeer;
175   }
176 }
177