1package eu.siacs.conversations.ui;
2
3import android.content.Intent;
4import android.content.pm.PackageManager;
5import android.net.Uri;
6import android.os.Bundle;
7import android.support.v7.widget.LinearLayoutManager;
8import android.support.v7.widget.RecyclerView;
9import android.util.Log;
10import android.view.Menu;
11import android.view.MenuItem;
12import android.widget.Toast;
13
14import java.util.ArrayList;
15import java.util.List;
16
17import eu.siacs.conversations.Config;
18import eu.siacs.conversations.R;
19import eu.siacs.conversations.entities.Account;
20import eu.siacs.conversations.entities.Conversation;
21import eu.siacs.conversations.services.XmppConnectionService;
22import eu.siacs.conversations.ui.adapter.ConversationAdapter;
23import eu.siacs.conversations.ui.service.EmojiService;
24import rocks.xmpp.addr.Jid;
25
26public class ShareWithActivity extends XmppActivity implements XmppConnectionService.OnConversationUpdate {
27
28 private static final int REQUEST_STORAGE_PERMISSION = 0x733f32;
29 private Conversation mPendingConversation = null;
30
31 @Override
32 public void onConversationUpdate() {
33 refreshUi();
34 }
35
36 private class Share {
37 ArrayList<Uri> uris = new ArrayList<>();
38 public String account;
39 public String contact;
40 public String text;
41 }
42
43 private Share share;
44
45 private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
46 private ConversationAdapter mAdapter;
47 private List<Conversation> mConversations = new ArrayList<>();
48
49
50 protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
51 super.onActivityResult(requestCode, resultCode, data);
52 if (requestCode == REQUEST_START_NEW_CONVERSATION
53 && resultCode == RESULT_OK) {
54 share.contact = data.getStringExtra("contact");
55 share.account = data.getStringExtra(EXTRA_ACCOUNT);
56 }
57 if (xmppConnectionServiceBound
58 && share != null
59 && share.contact != null
60 && share.account != null) {
61 share();
62 }
63 }
64
65 @Override
66 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
67 if (grantResults.length > 0)
68 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
69 if (requestCode == REQUEST_STORAGE_PERMISSION) {
70 if (this.mPendingConversation != null) {
71 share(this.mPendingConversation);
72 } else {
73 Log.d(Config.LOGTAG, "unable to find stored conversation");
74 }
75 }
76 } else {
77 Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
78 }
79 }
80
81 @Override
82 protected void onCreate(Bundle savedInstanceState) {
83 super.onCreate(savedInstanceState);
84 new EmojiService(this).init();
85
86 setContentView(R.layout.activity_share_with);
87
88 setSupportActionBar(findViewById(R.id.toolbar));
89 if (getSupportActionBar() != null) {
90 getSupportActionBar().setDisplayHomeAsUpEnabled(false);
91 getSupportActionBar().setHomeButtonEnabled(false);
92 }
93
94 setTitle(getString(R.string.title_activity_sharewith));
95
96 RecyclerView mListView = findViewById(R.id.choose_conversation_list);
97 mAdapter = new ConversationAdapter(this, this.mConversations);
98 mListView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
99 mListView.setAdapter(mAdapter);
100 mAdapter.setConversationClickListener((view, conversation) -> share(conversation));
101 this.share = new Share();
102 }
103
104 @Override
105 public boolean onCreateOptionsMenu(Menu menu) {
106 getMenuInflater().inflate(R.menu.share_with, menu);
107 return true;
108 }
109
110 @Override
111 public boolean onOptionsItemSelected(final MenuItem item) {
112 switch (item.getItemId()) {
113 case R.id.action_add:
114 final Intent intent = new Intent(getApplicationContext(), ChooseContactActivity.class);
115 intent.putExtra("direct_search",true);
116 startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
117 return true;
118 }
119 return super.onOptionsItemSelected(item);
120 }
121
122 @Override
123 public void onStart() {
124 super.onStart();
125 Intent intent = getIntent();
126 if (intent == null) {
127 return;
128 }
129 final String type = intent.getType();
130 final String action = intent.getAction();
131 if (Intent.ACTION_SEND.equals(action)) {
132 final String text = intent.getStringExtra(Intent.EXTRA_TEXT);
133 final Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
134 if (type != null && uri != null && (text == null || !type.equals("text/plain"))) {
135 this.share.uris.clear();
136 this.share.uris.add(uri);
137 } else {
138 this.share.text = text;
139 }
140 } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
141 this.share.uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
142 }
143 if (xmppConnectionServiceBound) {
144 xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0);
145 }
146
147 }
148
149 @Override
150 void onBackendConnected() {
151 if (xmppConnectionServiceBound && share != null && ((share.contact != null && share.account != null))) {
152 share();
153 return;
154 }
155 refreshUiReal();
156 }
157
158 private void share() {
159 final Conversation conversation;
160 Account account;
161 try {
162 account = xmppConnectionService.findAccountByJid(Jid.of(share.account));
163 } catch (final IllegalArgumentException e) {
164 account = null;
165 }
166 if (account == null) {
167 return;
168 }
169
170 try {
171 conversation = xmppConnectionService.findOrCreateConversation(account, Jid.of(share.contact), false, true);
172 } catch (final IllegalArgumentException e) {
173 return;
174 }
175 share(conversation);
176 }
177
178 private void share(final Conversation conversation) {
179 if (share.uris.size() != 0 && !hasStoragePermission(REQUEST_STORAGE_PERMISSION)) {
180 mPendingConversation = conversation;
181 return;
182 }
183 Intent intent = new Intent(this, ConversationsActivity.class);
184 intent.putExtra(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid());
185 if (share.uris.size() > 0) {
186 intent.setAction(Intent.ACTION_SEND_MULTIPLE);
187 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, share.uris);
188 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
189 } else if (share.text != null) {
190 intent.setAction(ConversationsActivity.ACTION_VIEW_CONVERSATION);
191 intent.putExtra(Intent.EXTRA_TEXT, share.text);
192 }
193 startActivity(intent);
194 finish();
195 }
196
197 public void refreshUiReal() {
198 xmppConnectionService.populateWithOrderedConversations(mConversations, this.share != null && this.share.uris.size() == 0);
199 mAdapter.notifyDataSetChanged();
200 }
201}