1 /*
2  * Copyright (C) 2010 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.tests.quickcontact;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.content.SharedPreferences;
22 import android.content.SharedPreferences.Editor;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.provider.ContactsContract.Contacts;
26 import android.provider.ContactsContract.QuickContact;
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.view.Window;
30 import android.widget.Button;
31 import android.widget.QuickContactBadge;
32 import android.widget.TextView;
33 
34 import com.android.contacts.tests.R;
35 
36 public class QuickContactTestsActivity extends Activity {
37     private static final int REQUEST_CODE_PICK = 1;
38     private static final String PREF_NAME = "quick_contact_prefs";
39     private static final String PREF_SETTING_URI = "uri";
40 
41     private Button mPickContact;
42     private TextView mUriTextView;
43     private QuickContactBadge mSmallBadge1;
44     private QuickContactBadge mSmallBadge2;
45     private QuickContactBadge mMediumBadge1;
46     private QuickContactBadge mMediumBadge2;
47     private QuickContactBadge mLargeBadge1;
48     private QuickContactBadge mLargeBadge2;
49     private Button mButton;
50 
51     private Uri mContactUri;
52 
53     @Override
onCreate(Bundle savedInstanceState)54     protected void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56 
57         getWindow().requestFeature(Window.FEATURE_NO_TITLE);
58         setContentView(R.layout.quick_contact_tests);
59 
60         mPickContact = (Button) findViewById(R.id.pick_contact);
61         mUriTextView = (TextView) findViewById(R.id.uri);
62         mSmallBadge1 = (QuickContactBadge) findViewById(R.id.small_badge1);
63         mSmallBadge2 = (QuickContactBadge) findViewById(R.id.small_badge2);
64         mMediumBadge1 = (QuickContactBadge) findViewById(R.id.medium_badge1);
65         mMediumBadge2 = (QuickContactBadge) findViewById(R.id.medium_badge2);
66         mLargeBadge1 = (QuickContactBadge) findViewById(R.id.large_badge1);
67         mLargeBadge2 = (QuickContactBadge) findViewById(R.id.large_badge2);
68         mButton = (Button) findViewById(android.R.id.button1);
69         mButton.setOnClickListener(new OnClickListener() {
70             @Override
71             public void onClick(View view) {
72                 final Intent intent = new Intent(QuickContact.ACTION_QUICK_CONTACT);
73                 intent.setData(mContactUri);
74                 startActivity(intent);
75             }
76         });
77 
78         mPickContact.setOnClickListener(new OnClickListener() {
79             @Override
80             public void onClick(View v) {
81                 final Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
82                 startActivityForResult(intent , REQUEST_CODE_PICK);
83             }
84         });
85 
86         // Load Uri if known
87         final SharedPreferences sharedPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
88         final String uriString = sharedPreferences.getString(PREF_SETTING_URI, null);
89         if (uriString != null) {
90             mContactUri = Uri.parse(uriString);
91             assignUri();
92         }
93     }
94 
95     @Override
onActivityResult(int requestCode, int resultCode, Intent data)96     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
97         super.onActivityResult(requestCode, resultCode, data);
98         if (resultCode == Activity.RESULT_CANCELED) return;
99         switch (requestCode) {
100             case REQUEST_CODE_PICK: {
101                 mContactUri = data.getData();
102                 assignUri();
103                 mButton.setEnabled(true);
104                 break;
105             }
106         }
107     }
108 
109     @Override
onDestroy()110     protected void onDestroy() {
111         super.onDestroy();
112 
113         final SharedPreferences sharedPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
114         final Editor editor = sharedPreferences.edit();
115         editor.putString(PREF_SETTING_URI, mContactUri == null ? null : mContactUri.toString());
116         editor.apply();
117     }
118 
assignUri()119     private void assignUri() {
120         mUriTextView.setText(mContactUri.toString());
121         mSmallBadge1.assignContactUri(mContactUri);
122         mSmallBadge2.assignContactUri(mContactUri);
123         mMediumBadge1.assignContactUri(mContactUri);
124         mMediumBadge2.assignContactUri(mContactUri);
125         mLargeBadge1.assignContactUri(mContactUri);
126         mLargeBadge2.assignContactUri(mContactUri);
127     }
128 }
129