ShareWithActivity.java

  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        setContentView(R.layout.activity_share_with);
 85
 86        setSupportActionBar(findViewById(R.id.toolbar));
 87        if (getSupportActionBar() != null) {
 88            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
 89            getSupportActionBar().setHomeButtonEnabled(false);
 90        }
 91
 92        setTitle(getString(R.string.title_activity_sharewith));
 93
 94        RecyclerView mListView = findViewById(R.id.choose_conversation_list);
 95        mAdapter = new ConversationAdapter(this, this.mConversations);
 96        mListView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
 97        mListView.setAdapter(mAdapter);
 98        mAdapter.setConversationClickListener((view, conversation) -> share(conversation));
 99        this.share = new Share();
100    }
101
102    @Override
103    public boolean onCreateOptionsMenu(Menu menu) {
104        getMenuInflater().inflate(R.menu.share_with, menu);
105        return true;
106    }
107
108    @Override
109    public boolean onOptionsItemSelected(final MenuItem item) {
110        switch (item.getItemId()) {
111            case R.id.action_add:
112                final Intent intent = new Intent(getApplicationContext(), ChooseContactActivity.class);
113                intent.putExtra("direct_search",true);
114                startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
115                return true;
116        }
117        return super.onOptionsItemSelected(item);
118    }
119
120    @Override
121    public void onStart() {
122        super.onStart();
123        Intent intent = getIntent();
124        if (intent == null) {
125            return;
126        }
127        final String type = intent.getType();
128        final String action = intent.getAction();
129        if (Intent.ACTION_SEND.equals(action)) {
130            final String text = intent.getStringExtra(Intent.EXTRA_TEXT);
131            final Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
132            if (type != null && uri != null && (text == null || !type.equals("text/plain"))) {
133                this.share.uris.clear();
134                this.share.uris.add(uri);
135            } else {
136                this.share.text = text;
137            }
138        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
139            this.share.uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
140        }
141        if (xmppConnectionServiceBound) {
142            xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0, false);
143        }
144
145    }
146
147    @Override
148    void onBackendConnected() {
149        if (xmppConnectionServiceBound && share != null && ((share.contact != null && share.account != null))) {
150            share();
151            return;
152        }
153        refreshUiReal();
154    }
155
156    private void share() {
157        final Conversation conversation;
158            Account account;
159            try {
160                account = xmppConnectionService.findAccountByJid(Jid.of(share.account));
161            } catch (final IllegalArgumentException e) {
162                account = null;
163            }
164            if (account == null) {
165                return;
166            }
167
168            try {
169                conversation = xmppConnectionService.findOrCreateConversation(account, Jid.of(share.contact), false, true);
170            } catch (final IllegalArgumentException e) {
171                return;
172            }
173        share(conversation);
174    }
175
176    private void share(final Conversation conversation) {
177        if (share.uris.size() != 0 && !hasStoragePermission(REQUEST_STORAGE_PERMISSION)) {
178            mPendingConversation = conversation;
179            return;
180        }
181        Intent intent = new Intent(this, ConversationsActivity.class);
182        intent.putExtra(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid());
183        if (share.uris.size() > 0) {
184            intent.setAction(Intent.ACTION_SEND_MULTIPLE);
185            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, share.uris);
186            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
187        } else if (share.text != null) {
188            intent.setAction(ConversationsActivity.ACTION_VIEW_CONVERSATION);
189            intent.putExtra(Intent.EXTRA_TEXT, share.text);
190        }
191        startActivity(intent);
192        finish();
193    }
194
195    public void refreshUiReal() {
196        //TODO inject desired order to not resort on refresh
197        xmppConnectionService.populateWithOrderedConversations(mConversations, this.share != null && this.share.uris.size() == 0, false);
198        mAdapter.notifyDataSetChanged();
199    }
200}