1 /*
2  * Copyright (C) 2015 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.example.android.directshare;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.AdapterView;
26 import android.widget.BaseAdapter;
27 import android.widget.ListAdapter;
28 import android.widget.ListView;
29 import android.widget.TextView;
30 
31 /**
32  * The dialog for selecting a contact to share the text with. This dialog is shown when the user
33  * taps on this sample's icon rather than any of the Direct Share contacts.
34  */
35 public class SelectContactActivity extends Activity {
36 
37     /**
38      * The action string for Intents.
39      */
40     public static final String ACTION_SELECT_CONTACT
41             = "com.example.android.directshare.intent.action.SELECT_CONTACT";
42 
43     @Override
onCreate(Bundle savedInstanceState)44     protected void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         setContentView(R.layout.select_contact);
47         Intent intent = getIntent();
48         if (!ACTION_SELECT_CONTACT.equals(intent.getAction())) {
49             finish();
50             return;
51         }
52         // Set up the list of contacts
53         ListView list = (ListView) findViewById(R.id.list);
54         list.setAdapter(mAdapter);
55         list.setOnItemClickListener(mOnItemClickListener);
56     }
57 
58     private final ListAdapter mAdapter = new BaseAdapter() {
59         @Override
60         public int getCount() {
61             return Contact.CONTACTS.length;
62         }
63 
64         @Override
65         public Object getItem(int i) {
66             return Contact.byId(i);
67         }
68 
69         @Override
70         public long getItemId(int i) {
71             return i;
72         }
73 
74         @Override
75         public View getView(int i, View view, ViewGroup parent) {
76             if (view == null) {
77                 view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact, parent,
78                         false);
79             }
80             TextView textView = (TextView) view;
81             Contact contact = (Contact) getItem(i);
82             ContactViewBinder.bind(contact, textView);
83             return textView;
84         }
85     };
86 
87     private final AdapterView.OnItemClickListener mOnItemClickListener
88             = new AdapterView.OnItemClickListener() {
89         @Override
90         public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
91             Intent data = new Intent();
92             data.putExtra(Contact.ID, i);
93             setResult(RESULT_OK, data);
94             finish();
95         }
96     };
97 
98 }
99