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 package com.android.messaging.ui.conversationlist;
17 
18 import android.app.Activity;
19 import android.app.AlertDialog;
20 import android.app.AlertDialog.Builder;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.content.DialogInterface;
24 import android.content.DialogInterface.OnClickListener;
25 import android.database.Cursor;
26 import android.os.Bundle;
27 import androidx.recyclerview.widget.LinearLayoutManager;
28 import androidx.recyclerview.widget.RecyclerView;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 
33 import com.android.messaging.R;
34 import com.android.messaging.datamodel.binding.Binding;
35 import com.android.messaging.datamodel.binding.BindingBase;
36 import com.android.messaging.datamodel.data.ConversationListData;
37 import com.android.messaging.datamodel.data.ConversationListItemData;
38 import com.android.messaging.datamodel.data.ConversationListData.ConversationListDataListener;
39 import com.android.messaging.ui.ListEmptyView;
40 import com.android.messaging.datamodel.DataModel;
41 
42 /**
43  * Allow user to pick conversation to which an incoming attachment will be shared.
44  */
45 public class ShareIntentFragment extends DialogFragment implements ConversationListDataListener,
46         ShareIntentAdapter.HostInterface {
47     public static final String HIDE_NEW_CONVERSATION_BUTTON_KEY = "hide_conv_button_key";
48 
49     public interface HostInterface {
onConversationClick(final ConversationListItemData conversationListItemData)50         public void onConversationClick(final ConversationListItemData conversationListItemData);
onCreateConversationClick()51         public void onCreateConversationClick();
52     }
53 
54     private final Binding<ConversationListData> mListBinding = BindingBase.createBinding(this);
55     private RecyclerView mRecyclerView;
56     private ListEmptyView mEmptyListMessageView;
57     private ShareIntentAdapter mAdapter;
58     private HostInterface mHost;
59     private boolean mDismissed;
60 
61     /**
62      * {@inheritDoc} from Fragment
63      */
64     @Override
onCreateDialog(final Bundle bundle)65     public Dialog onCreateDialog(final Bundle bundle) {
66         final Activity activity = getActivity();
67         final LayoutInflater inflater = activity.getLayoutInflater();
68         View view = inflater.inflate(R.layout.share_intent_conversation_list_view, null);
69         mEmptyListMessageView = (ListEmptyView) view.findViewById(R.id.no_conversations_view);
70         mEmptyListMessageView.setImageHint(R.drawable.ic_oobe_conv_list);
71         // The default behavior for default layout param generation by LinearLayoutManager is to
72         // provide width and height of WRAP_CONTENT, but this is not desirable for
73         // ShareIntentFragment; the view in each row should be a width of MATCH_PARENT so that
74         // the entire row is tappable.
75         final LinearLayoutManager manager = new LinearLayoutManager(activity) {
76             @Override
77             public RecyclerView.LayoutParams generateDefaultLayoutParams() {
78                 return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
79                         ViewGroup.LayoutParams.WRAP_CONTENT);
80             }
81         };
82         mListBinding.getData().init(getLoaderManager(), mListBinding);
83         mAdapter = new ShareIntentAdapter(activity, null, this);
84         mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
85         mRecyclerView.setLayoutManager(manager);
86         mRecyclerView.setHasFixedSize(true);
87         mRecyclerView.setAdapter(mAdapter);
88         final Builder dialogBuilder = new AlertDialog.Builder(activity)
89                 .setView(view)
90                 .setTitle(R.string.share_intent_activity_label);
91 
92         final Bundle arguments = getArguments();
93         if (arguments == null || !arguments.getBoolean(HIDE_NEW_CONVERSATION_BUTTON_KEY)) {
94             dialogBuilder.setPositiveButton(R.string.share_new_message, new OnClickListener() {
95                         @Override
96                     public void onClick(DialogInterface dialog, int which) {
97                             mDismissed = true;
98                             mHost.onCreateConversationClick();
99                     }
100                 });
101         }
102         return dialogBuilder.setNegativeButton(R.string.share_cancel, null)
103                 .create();
104     }
105 
106     @Override
onDismiss(DialogInterface dialog)107     public void onDismiss(DialogInterface dialog) {
108         if (!mDismissed) {
109             final Activity activity = getActivity();
110             if (activity != null) {
111                 activity.finish();
112             }
113         }
114     }
115 
116     /**
117      * {@inheritDoc} from Fragment
118      */
119     @Override
onDestroy()120     public void onDestroy() {
121         super.onDestroy();
122         mListBinding.unbind();
123     }
124 
125     @Override
onAttach(final Activity activity)126     public void onAttach(final Activity activity) {
127         super.onAttach(activity);
128         if (activity instanceof HostInterface) {
129             mHost = (HostInterface) activity;
130         }
131         mListBinding.bind(DataModel.get().createConversationListData(activity, this, false));
132     }
133 
134     @Override
onConversationListCursorUpdated(final ConversationListData data, final Cursor cursor)135     public void onConversationListCursorUpdated(final ConversationListData data,
136             final Cursor cursor) {
137         mListBinding.ensureBound(data);
138         mAdapter.swapCursor(cursor);
139         updateEmptyListUi(cursor == null || cursor.getCount() == 0);
140     }
141 
142     /**
143      * {@inheritDoc} from SharIntentItemView.HostInterface
144      */
145     @Override
onConversationClicked(final ConversationListItemData conversationListItemData)146     public void onConversationClicked(final ConversationListItemData conversationListItemData) {
147         mHost.onConversationClick(conversationListItemData);
148     }
149 
150     // Show and hide empty list UI as needed with appropriate text based on view specifics
updateEmptyListUi(final boolean isEmpty)151     private void updateEmptyListUi(final boolean isEmpty) {
152         if (isEmpty) {
153             mEmptyListMessageView.setTextHint(R.string.conversation_list_empty_text);
154             mEmptyListMessageView.setVisibility(View.VISIBLE);
155         } else {
156             mEmptyListMessageView.setVisibility(View.GONE);
157         }
158     }
159 
160     @Override
setBlockedParticipantsAvailable(boolean blockedAvailable)161     public void setBlockedParticipantsAvailable(boolean blockedAvailable) {
162     }
163 }
164