Make PeopleActivity extends AppCompatActivity (1/3)
This CL includes a subset of reversion of ag/871001.
This topic of CLs will lead to the following changes in APK sizes:
- GoogleContacts: 8.0MB --> 8.7MB
- GoogleContacts-N: 7.6MB --> 8.2MB
Bug 27687799
Change-Id: Id94598e42a5016badeafee3f4f0dc2a515fc68eb
diff --git a/src/com/android/contacts/AppCompatContactsActivity.java b/src/com/android/contacts/AppCompatContactsActivity.java
new file mode 100644
index 0000000..7654b27
--- /dev/null
+++ b/src/com/android/contacts/AppCompatContactsActivity.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2016 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.contacts;
+
+import android.app.Fragment;
+import android.app.FragmentManager;
+import android.app.FragmentTransaction;
+import android.content.ContentResolver;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.view.View;
+
+import com.android.contacts.common.activity.AppCompatTransactionSafeActivity;
+import com.android.contacts.common.testing.InjectedServices;
+
+/**
+ * A common superclass for Contacts activities that handles application-wide services, copied from
+ * {@link com.android.contacts.ContactsActivity}, which will be deprecated after Kitkat backporting
+ * is done.
+ */
+public abstract class AppCompatContactsActivity extends AppCompatTransactionSafeActivity
+ implements ContactSaveService.Listener {
+
+ private ContentResolver mContentResolver;
+
+ @Override
+ public ContentResolver getContentResolver() {
+ if (mContentResolver == null) {
+ InjectedServices services = ContactsApplication.getInjectedServices();
+ if (services != null) {
+ mContentResolver = services.getContentResolver();
+ }
+ if (mContentResolver == null) {
+ mContentResolver = super.getContentResolver();
+ }
+ }
+ return mContentResolver;
+ }
+
+ @Override
+ public SharedPreferences getSharedPreferences(String name, int mode) {
+ InjectedServices services = ContactsApplication.getInjectedServices();
+ if (services != null) {
+ SharedPreferences prefs = services.getSharedPreferences();
+ if (prefs != null) {
+ return prefs;
+ }
+ }
+
+ return super.getSharedPreferences(name, mode);
+ }
+
+ @Override
+ public Object getSystemService(String name) {
+ Object service = super.getSystemService(name);
+ if (service != null) {
+ return service;
+ }
+
+ return getApplicationContext().getSystemService(name);
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ ContactSaveService.registerListener(this);
+ super.onCreate(savedInstanceState);
+ }
+
+ @Override
+ protected void onDestroy() {
+ ContactSaveService.unregisterListener(this);
+ super.onDestroy();
+ }
+
+ @Override
+ public void onServiceCompleted(Intent callbackIntent) {
+ onNewIntent(callbackIntent);
+ }
+
+ /**
+ * Convenient version of {@link FragmentManager#findFragmentById(int)}, which throws
+ * an exception if the fragment doesn't exist.
+ */
+ @SuppressWarnings("unchecked")
+ public <T extends Fragment> T getFragment(int id) {
+ T result = (T)getFragmentManager().findFragmentById(id);
+ if (result == null) {
+ throw new IllegalArgumentException("fragment 0x" + Integer.toHexString(id)
+ + " doesn't exist");
+ }
+ return result;
+ }
+
+ /**
+ * Convenient version of {@link #findViewById(int)}, which throws
+ * an exception if the view doesn't exist.
+ */
+ @SuppressWarnings("unchecked")
+ public <T extends View> T getView(int id) {
+ T result = (T)findViewById(id);
+ if (result == null) {
+ throw new IllegalArgumentException("view 0x" + Integer.toHexString(id)
+ + " doesn't exist");
+ }
+ return result;
+ }
+
+ protected static void showFragment(FragmentTransaction ft, Fragment f) {
+ if ((f != null) && f.isHidden()) ft.show(f);
+ }
+
+ protected static void hideFragment(FragmentTransaction ft, Fragment f) {
+ if ((f != null) && !f.isHidden()) ft.hide(f);
+ }
+}
diff --git a/src/com/android/contacts/activities/ActionBarAdapter.java b/src/com/android/contacts/activities/ActionBarAdapter.java
index 9862b6f..ad70d92 100644
--- a/src/com/android/contacts/activities/ActionBarAdapter.java
+++ b/src/com/android/contacts/activities/ActionBarAdapter.java
@@ -17,7 +17,6 @@
package com.android.contacts.activities;
import android.animation.ValueAnimator;
-import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
@@ -25,6 +24,8 @@
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
+import android.support.v7.app.ActionBar;
+import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
@@ -39,7 +40,6 @@
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
-import android.widget.Toolbar;
import com.android.contacts.R;
import com.android.contacts.activities.ActionBarAdapter.Listener.Action;
diff --git a/src/com/android/contacts/activities/PeopleActivity.java b/src/com/android/contacts/activities/PeopleActivity.java
index d349462..a26148a 100644
--- a/src/com/android/contacts/activities/PeopleActivity.java
+++ b/src/com/android/contacts/activities/PeopleActivity.java
@@ -37,6 +37,7 @@
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
+import android.support.v7.widget.Toolbar;
import android.telecom.TelecomManager;
import android.text.TextUtils;
import android.util.Log;
@@ -47,12 +48,11 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
-import android.view.Window;
import android.widget.ImageButton;
import android.widget.Toast;
-import android.widget.Toolbar;
-import com.android.contacts.ContactsActivity;
+
+import com.android.contacts.AppCompatContactsActivity;
import com.android.contacts.R;
import com.android.contacts.activities.ActionBarAdapter.TabState;
import com.android.contacts.common.ContactsUtils;
@@ -104,7 +104,7 @@
/**
* Displays a list to browse contacts.
*/
-public class PeopleActivity extends ContactsActivity implements
+public class PeopleActivity extends AppCompatContactsActivity implements
View.OnCreateContextMenuListener,
View.OnClickListener,
ActionBarAdapter.Listener,
@@ -299,10 +299,6 @@
}
private void createViewsAndFragments(Bundle savedState) {
- // Disable the ActionBar so that we can use a Toolbar. This needs to be called before
- // setContentView().
- getWindow().requestFeature(Window.FEATURE_NO_TITLE);
-
setContentView(R.layout.people_activity);
final FragmentManager fragmentManager = getFragmentManager();
@@ -318,9 +314,9 @@
mTabPager.setAdapter(mTabPagerAdapter);
mTabPager.setOnPageChangeListener(mTabPagerListener);
- // Configure toolbar and toolbar tabs. If in landscape mode, we configure tabs differntly.
+ // Configure toolbar and toolbar tabs. If in landscape mode, we configure tabs differently.
final Toolbar toolbar = getView(R.id.toolbar);
- setActionBar(toolbar);
+ setSupportActionBar(toolbar);
final ViewPagerTabs portraitViewPagerTabs
= (ViewPagerTabs) findViewById(R.id.lists_pager_header);
ViewPagerTabs landscapeViewPagerTabs = null;
@@ -371,7 +367,7 @@
// Setting Properties after fragment is created
mFavoritesFragment.setDisplayType(DisplayType.STREQUENT);
- mActionBarAdapter = new ActionBarAdapter(this, this, getActionBar(),
+ mActionBarAdapter = new ActionBarAdapter(this, this, getSupportActionBar(),
portraitViewPagerTabs, landscapeViewPagerTabs, toolbar);
mActionBarAdapter.initialize(savedState, mRequest);