158 lines
5.2 KiB
Java
158 lines
5.2 KiB
Java
/*
|
|
* Copyright (C) 2015 Open Whisper Systems
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package org.thoughtcrime.securesms;
|
|
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.view.Menu;
|
|
import android.view.MenuItem;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import org.signal.core.util.logging.Log;
|
|
import org.thoughtcrime.securesms.contacts.sync.DirectoryHelper;
|
|
import org.thoughtcrime.securesms.conversation.ConversationIntents;
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
import org.thoughtcrime.securesms.groups.ui.creategroup.CreateGroupActivity;
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
import org.thoughtcrime.securesms.recipients.RecipientId;
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
import org.thoughtcrime.securesms.util.concurrent.SimpleTask;
|
|
import org.thoughtcrime.securesms.util.views.SimpleProgressDialog;
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* Activity container for starting a new conversation.
|
|
*
|
|
* @author Moxie Marlinspike
|
|
*
|
|
*/
|
|
public class NewConversationActivity extends ContactSelectionActivity
|
|
implements ContactSelectionListFragment.ListCallback
|
|
{
|
|
|
|
@SuppressWarnings("unused")
|
|
private static final String TAG = NewConversationActivity.class.getSimpleName();
|
|
|
|
@Override
|
|
public void onCreate(Bundle bundle, boolean ready) {
|
|
super.onCreate(bundle, ready);
|
|
assert getSupportActionBar() != null;
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
}
|
|
|
|
@Override
|
|
public boolean onBeforeContactSelected(Optional<RecipientId> recipientId, String number) {
|
|
if (recipientId.isPresent()) {
|
|
launch(Recipient.resolved(recipientId.get()));
|
|
} else {
|
|
Log.i(TAG, "[onContactSelected] Maybe creating a new recipient.");
|
|
|
|
if (TextSecurePreferences.isPushRegistered(this) && NetworkConstraint.isMet(this)) {
|
|
Log.i(TAG, "[onContactSelected] Doing contact refresh.");
|
|
|
|
AlertDialog progress = SimpleProgressDialog.show(this);
|
|
|
|
SimpleTask.run(getLifecycle(), () -> {
|
|
Recipient resolved = Recipient.external(this, number);
|
|
|
|
if (!resolved.isRegistered() || !resolved.hasUuid()) {
|
|
Log.i(TAG, "[onContactSelected] Not registered or no UUID. Doing a directory refresh.");
|
|
try {
|
|
DirectoryHelper.refreshDirectoryFor(this, resolved, false);
|
|
resolved = Recipient.resolved(resolved.getId());
|
|
} catch (IOException e) {
|
|
Log.w(TAG, "[onContactSelected] Failed to refresh directory for new contact.");
|
|
}
|
|
}
|
|
|
|
return resolved;
|
|
}, resolved -> {
|
|
progress.dismiss();
|
|
launch(resolved);
|
|
});
|
|
} else {
|
|
launch(Recipient.external(this, number));
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private void launch(Recipient recipient) {
|
|
long existingThread = DatabaseFactory.getThreadDatabase(this).getThreadIdIfExistsFor(recipient.getId());
|
|
Intent intent = ConversationIntents.createBuilder(this, recipient.getId(), existingThread)
|
|
.withDraftText(getIntent().getStringExtra(Intent.EXTRA_TEXT))
|
|
.withDataUri(getIntent().getData())
|
|
.withDataType(getIntent().getType())
|
|
.build();
|
|
|
|
startActivity(intent);
|
|
finish();
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
super.onOptionsItemSelected(item);
|
|
|
|
switch (item.getItemId()) {
|
|
case android.R.id.home: super.onBackPressed(); return true;
|
|
case R.id.menu_refresh: handleManualRefresh(); return true;
|
|
case R.id.menu_new_group: handleCreateGroup(); return true;
|
|
case R.id.menu_invite: handleInvite(); return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void handleManualRefresh() {
|
|
contactsFragment.setRefreshing(true);
|
|
onRefresh();
|
|
}
|
|
|
|
private void handleCreateGroup() {
|
|
startActivity(CreateGroupActivity.newIntent(this));
|
|
}
|
|
|
|
private void handleInvite() {
|
|
startActivity(new Intent(this, InviteActivity.class));
|
|
}
|
|
|
|
@Override
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
menu.clear();
|
|
getMenuInflater().inflate(R.menu.new_conversation_activity, menu);
|
|
|
|
super.onCreateOptionsMenu(menu);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void onInvite() {
|
|
handleInvite();
|
|
finish();
|
|
}
|
|
|
|
@Override
|
|
public void onNewGroup(boolean forceV1) {
|
|
handleCreateGroup();
|
|
finish();
|
|
}
|
|
}
|