Home | History | Annotate | Download | only in activities
      1 /*
      2  * Copyright (C) 2009 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.activities;
     18 
     19 
     20 import android.app.ActionBar;
     21 import android.app.ActionBar.LayoutParams;
     22 import android.app.Fragment;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.net.Uri;
     26 import android.os.Bundle;
     27 import android.provider.ContactsContract;
     28 import android.text.TextUtils;
     29 import android.util.Log;
     30 import android.view.LayoutInflater;
     31 import android.view.MenuItem;
     32 import android.view.View;
     33 import android.view.View.OnFocusChangeListener;
     34 import android.view.inputmethod.InputMethodManager;
     35 import android.widget.SearchView;
     36 import android.widget.SearchView.OnCloseListener;
     37 import android.widget.SearchView.OnQueryTextListener;
     38 
     39 import com.android.contacts.ContactsActivity;
     40 import com.android.contacts.R;
     41 import com.android.contacts.common.list.ContactEntryListFragment;
     42 import com.android.contacts.list.JoinContactListFragment;
     43 import com.android.contacts.list.OnContactPickerActionListener;
     44 
     45 /**
     46  * An activity that shows a list of contacts that can be joined with the target contact.
     47  */
     48 public class JoinContactActivity extends ContactsActivity
     49         implements OnQueryTextListener, OnCloseListener, OnFocusChangeListener {
     50 
     51     private static final String TAG = "JoinContactActivity";
     52 
     53     /**
     54      * The action for the join contact activity.
     55      * <p>
     56      * Input: extra field {@link #EXTRA_TARGET_CONTACT_ID} is the aggregate ID.
     57      * TODO: move to {@link ContactsContract}.
     58      */
     59     public static final String JOIN_CONTACT = "com.android.contacts.action.JOIN_CONTACT";
     60 
     61     /**
     62      * Used with {@link #JOIN_CONTACT} to give it the target for aggregation.
     63      * <p>
     64      * Type: LONG
     65      */
     66     public static final String EXTRA_TARGET_CONTACT_ID = "com.android.contacts.action.CONTACT_ID";
     67 
     68     private static final String KEY_TARGET_CONTACT_ID = "targetContactId";
     69 
     70     private long mTargetContactId;
     71 
     72     private JoinContactListFragment mListFragment;
     73     private SearchView mSearchView;
     74 
     75     @Override
     76     public void onAttachFragment(Fragment fragment) {
     77         if (fragment instanceof JoinContactListFragment) {
     78             mListFragment = (JoinContactListFragment) fragment;
     79             setupActionListener();
     80         }
     81     }
     82 
     83     @Override
     84     protected void onCreate(Bundle savedInstanceState) {
     85         super.onCreate(savedInstanceState);
     86 
     87         Intent intent = getIntent();
     88         mTargetContactId = intent.getLongExtra(EXTRA_TARGET_CONTACT_ID, -1);
     89         if (mTargetContactId == -1) {
     90             Log.e(TAG, "Intent " + intent.getAction() + " is missing required extra: "
     91                     + EXTRA_TARGET_CONTACT_ID);
     92             setResult(RESULT_CANCELED);
     93             finish();
     94             return;
     95         }
     96 
     97         setContentView(R.layout.join_contact_picker);
     98         setTitle(R.string.titleJoinContactDataWith);
     99 
    100         if (mListFragment == null) {
    101             mListFragment = new JoinContactListFragment();
    102 
    103             getFragmentManager().beginTransaction()
    104                     .replace(R.id.list_container, mListFragment)
    105                     .commitAllowingStateLoss();
    106         }
    107 
    108         prepareSearchViewAndActionBar();
    109     }
    110 
    111     private void setupActionListener() {
    112         mListFragment.setTargetContactId(mTargetContactId);
    113         mListFragment.setOnContactPickerActionListener(new OnContactPickerActionListener() {
    114             @Override
    115             public void onPickContactAction(Uri contactUri) {
    116                 Intent intent = new Intent(null, contactUri);
    117                 setResult(RESULT_OK, intent);
    118                 finish();
    119             }
    120 
    121             @Override
    122             public void onShortcutIntentCreated(Intent intent) {
    123             }
    124 
    125             @Override
    126             public void onCreateNewContactAction() {
    127             }
    128 
    129             @Override
    130             public void onEditContactAction(Uri contactLookupUri) {
    131             }
    132         });
    133     }
    134 
    135     private void prepareSearchViewAndActionBar() {
    136         final ActionBar actionBar = getActionBar();
    137         if (actionBar != null) {
    138             final View searchViewOnLayout = findViewById(R.id.search_view);
    139             if (searchViewOnLayout != null) {
    140                 searchViewOnLayout.setVisibility(View.GONE);
    141             }
    142 
    143             final View searchViewLayout = LayoutInflater.from(actionBar.getThemedContext())
    144                     .inflate(R.layout.custom_action_bar, null);
    145             mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view);
    146 
    147             // In order to make the SearchView look like "shown via search menu", we need to
    148             // manually setup its state. See also DialtactsActivity.java and ActionBarAdapter.java.
    149             mSearchView.setIconifiedByDefault(true);
    150             mSearchView.setQueryHint(getString(R.string.hint_findContacts));
    151             mSearchView.setIconified(false);
    152 
    153             mSearchView.setOnQueryTextListener(this);
    154             mSearchView.setOnCloseListener(this);
    155             mSearchView.setOnQueryTextFocusChangeListener(this);
    156 
    157             actionBar.setCustomView(searchViewLayout,
    158                     new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    159             actionBar.setDisplayShowCustomEnabled(true);
    160             actionBar.setDisplayShowHomeEnabled(true);
    161             actionBar.setDisplayHomeAsUpEnabled(true);
    162         } else {
    163             mSearchView = (SearchView) findViewById(R.id.search_view);
    164             mSearchView.setQueryHint(getString(R.string.hint_findContacts));
    165             mSearchView.setOnQueryTextListener(this);
    166             mSearchView.setOnQueryTextFocusChangeListener(this);
    167         }
    168 
    169         // Clear focus and suppress keyboard show-up.
    170         mSearchView.clearFocus();
    171     }
    172 
    173     @Override
    174     public boolean onQueryTextChange(String newText) {
    175         mListFragment.setQueryString(newText, true);
    176         return false;
    177     }
    178 
    179     @Override
    180     public boolean onQueryTextSubmit(String query) {
    181         return false;
    182     }
    183 
    184     @Override
    185     public boolean onClose() {
    186         if (!TextUtils.isEmpty(mSearchView.getQuery())) {
    187             mSearchView.setQuery(null, true);
    188         }
    189         return true;
    190     }
    191 
    192     @Override
    193     public void onFocusChange(View view, boolean hasFocus) {
    194         switch (view.getId()) {
    195             case R.id.search_view: {
    196                 if (hasFocus) {
    197                     showInputMethod(mSearchView.findFocus());
    198                 }
    199             }
    200         }
    201     }
    202 
    203     @Override
    204     public boolean onOptionsItemSelected(MenuItem item) {
    205         switch (item.getItemId()) {
    206             case android.R.id.home:
    207                 // Go back to previous screen, intending "cancel"
    208                 setResult(RESULT_CANCELED);
    209                 finish();
    210                 return true;
    211         }
    212         return super.onOptionsItemSelected(item);
    213     }
    214 
    215     @Override
    216     protected void onSaveInstanceState(Bundle outState) {
    217         super.onSaveInstanceState(outState);
    218         outState.putLong(KEY_TARGET_CONTACT_ID, mTargetContactId);
    219     }
    220 
    221     @Override
    222     protected void onRestoreInstanceState(Bundle savedInstanceState) {
    223         super.onRestoreInstanceState(savedInstanceState);
    224         mTargetContactId = savedInstanceState.getLong(KEY_TARGET_CONTACT_ID);
    225     }
    226 
    227     @Override
    228     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    229         if (requestCode == ContactEntryListFragment.ACTIVITY_REQUEST_CODE_PICKER
    230                 && resultCode == RESULT_OK) {
    231             mListFragment.onPickerResult(data);
    232         }
    233     }
    234 
    235     private void showInputMethod(View view) {
    236         final InputMethodManager imm = (InputMethodManager)
    237                 getSystemService(Context.INPUT_METHOD_SERVICE);
    238         if (imm != null) {
    239             if (!imm.showSoftInput(view, 0)) {
    240                 Log.w(TAG, "Failed to show soft input method.");
    241             }
    242         }
    243     }
    244 }
    245