Home | History | Annotate | Download | only in contacts
      1 /*
      2  * Copyright (C) 2016 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.contacts;
     18 
     19 import android.app.Fragment;
     20 import android.app.FragmentManager;
     21 import android.app.FragmentTransaction;
     22 import android.content.ContentResolver;
     23 import android.content.Intent;
     24 import android.content.SharedPreferences;
     25 import android.os.Bundle;
     26 import android.view.View;
     27 
     28 import com.android.contacts.activities.AppCompatTransactionSafeActivity;
     29 import com.android.contacts.testing.InjectedServices;
     30 
     31 /**
     32  * A common superclass for Contacts activities that handles application-wide services, copied from
     33  * {@link com.android.contacts.ContactsActivity}, which will be deprecated after Kitkat backporting
     34  * is done.
     35  */
     36 public abstract class AppCompatContactsActivity extends AppCompatTransactionSafeActivity
     37         implements ContactSaveService.Listener {
     38 
     39     private ContentResolver mContentResolver;
     40 
     41     @Override
     42     public ContentResolver getContentResolver() {
     43         if (mContentResolver == null) {
     44             InjectedServices services = ContactsApplication.getInjectedServices();
     45             if (services != null) {
     46                 mContentResolver = services.getContentResolver();
     47             }
     48             if (mContentResolver == null) {
     49                 mContentResolver = super.getContentResolver();
     50             }
     51         }
     52         return mContentResolver;
     53     }
     54 
     55     @Override
     56     public SharedPreferences getSharedPreferences(String name, int mode) {
     57         InjectedServices services = ContactsApplication.getInjectedServices();
     58         if (services != null) {
     59             SharedPreferences prefs = services.getSharedPreferences();
     60             if (prefs != null) {
     61                 return prefs;
     62             }
     63         }
     64 
     65         return super.getSharedPreferences(name, mode);
     66     }
     67 
     68     @Override
     69     public Object getSystemService(String name) {
     70         Object service = super.getSystemService(name);
     71         if (service != null) {
     72             return service;
     73         }
     74 
     75         return getApplicationContext().getSystemService(name);
     76     }
     77 
     78     @Override
     79     protected void onCreate(Bundle savedInstanceState) {
     80         ContactSaveService.registerListener(this);
     81         super.onCreate(savedInstanceState);
     82     }
     83 
     84     @Override
     85     protected void onDestroy() {
     86         ContactSaveService.unregisterListener(this);
     87         super.onDestroy();
     88     }
     89 
     90     @Override
     91     public void onServiceCompleted(Intent callbackIntent) {
     92         onNewIntent(callbackIntent);
     93     }
     94 
     95     /**
     96      * Convenient version of {@link FragmentManager#findFragmentById(int)}, which throws
     97      * an exception if the fragment doesn't exist.
     98      */
     99     @SuppressWarnings("unchecked")
    100     public <T extends Fragment> T getFragment(int id) {
    101         T result = (T)getFragmentManager().findFragmentById(id);
    102         if (result == null) {
    103             throw new IllegalArgumentException("fragment 0x" + Integer.toHexString(id)
    104                     + " doesn't exist");
    105         }
    106         return result;
    107     }
    108 
    109     /**
    110      * Convenient version of {@link #findViewById(int)}, which throws
    111      * an exception if the view doesn't exist.
    112      */
    113     @SuppressWarnings("unchecked")
    114     public <T extends View> T getView(int id) {
    115         T result = (T)findViewById(id);
    116         if (result == null) {
    117             throw new IllegalArgumentException("view 0x" + Integer.toHexString(id)
    118                     + " doesn't exist");
    119         }
    120         return result;
    121     }
    122 
    123     protected static void showFragment(FragmentTransaction ft, Fragment f) {
    124         if ((f != null) && f.isHidden()) ft.show(f);
    125     }
    126 
    127     protected static void hideFragment(FragmentTransaction ft, Fragment f) {
    128         if ((f != null) && !f.isHidden()) ft.hide(f);
    129     }
    130 }
    131