Skip to content

Commit

Permalink
Merge pull request #35 from loki-project/custom-server
Browse files Browse the repository at this point in the history
Custom Public Chats
  • Loading branch information
nielsandriesse committed Oct 15, 2019
2 parents 9aafedd + a5b543c commit e8e539c
Show file tree
Hide file tree
Showing 28 changed files with 536 additions and 159 deletions.
4 changes: 4 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@
<activity android:name="org.thoughtcrime.securesms.loki.NewConversationActivity"
android:windowSoftInputMode="stateAlwaysVisible"
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize" />

<activity android:name="org.thoughtcrime.securesms.loki.AddPublicChatActivity"
android:windowSoftInputMode="stateAlwaysVisible"
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize" />
<!-- Loki -->

<service android:enabled="true" android:name="org.thoughtcrime.securesms.service.WebRtcCallService"/>
Expand Down
48 changes: 48 additions & 0 deletions res/layout/activity_add_public_chat.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:orientation="vertical">

<org.thoughtcrime.securesms.components.LabeledEditText
android:id="@+id/urlEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:labeledEditText_background="@color/loki_darkest_gray"
app:labeledEditText_label="@string/fragment_add_public_chat_url_edit_text_label"/>

<TextView
android:id="@+id/explanationTextView"
style="@style/Signal.Text.Body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/fragment_add_public_chat_explanation" />

<com.dd.CircularProgressButton
android:id="@+id/addButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="32dp"
android:layout_gravity="center_horizontal"
android:background="@color/signal_primary"
android:textColor="@color/white"
app:cpb_colorIndicator="@color/white"
app:cpb_colorProgress="@color/textsecure_primary"
app:cpb_cornerRadius="4dp"
app:cpb_selectorIdle="@drawable/progress_button_state"
app:cpb_textIdle="@string/fragment_add_public_chat_add_button_title_1" />

</LinearLayout>

</ScrollView>
12 changes: 9 additions & 3 deletions res/menu/text_secure_normal.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:title="@string/text_secure_normal__menu_new_group"
<item android:title="@string/activity_conversation_list_add_public_chat_button_title"
android:id="@+id/menu_conversation_list_add_public_chat_option"
android:icon="@drawable/ic_group_white_24dp"
app:showAsAction="always" />

<!-- <item android:title="@string/text_secure_normal__menu_new_group"
android:id="@+id/menu_new_group" />
<item android:title="@string/text_secure_normal__menu_clear_passphrase"
Expand All @@ -17,6 +23,6 @@
android:id="@+id/menu_settings" />
<item android:title="@string/text_secure_normal__help"
android:id="@+id/menu_help"/>
android:id="@+id/menu_help"/> -->

</menu>
10 changes: 10 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,14 @@
<string name="fragment_new_conversation_next_button_title">Next</string>
<string name="fragment_new_conversation_invalid_public_key_message">Invalid public key</string>
<string name="fragment_new_conversation_note_to_self_not_supported_message">Please enter the public key of the person you\'d like to message</string>
<!-- Add public chat activity -->
<string name="fragment_add_public_chat_title">Add Public Chat</string>
<string name="fragment_add_public_chat_url_edit_text_label">URL</string>
<string name="fragment_add_public_chat_explanation">Enter the URL of the public chat you\'d like to join. The Loki Public Chat URL is https://chat.lokinet.org.</string>
<string name="fragment_add_public_chat_add_button_title_1">Add</string>
<string name="fragment_add_public_chat_add_button_title_2">Adding Server...</string>
<string name="fragment_add_public_chat_invalid_url_message">Invalid URL</string>
<string name="fragment_add_public_chat_connection_failed_message">Couldn\'t Connect</string>
<!-- Friend request view -->
<string name="view_friend_request_accept_button_title">Accept</string>
<string name="view_friend_request_reject_button_title">Reject</string>
Expand Down Expand Up @@ -1631,5 +1639,7 @@
<string name="fragment_scan_qr_code_camera_permission_dialog_message">Loki Messenger needs camera access to scan QR codes.</string>
<!-- Conversation activity -->
<string name="activity_conversation_copy_public_key_button_title">Copy public key</string>
<!-- Conversation list activity -->
<string name="activity_conversation_list_add_public_chat_button_title">Add Public Chat</string>

</resources>
4 changes: 2 additions & 2 deletions res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
android:icon="@drawable/icon_qr_code"/>

<Preference android:key="preference_category_link_device"
android:title="@string/activity_settings_link_device_button_title"
android:icon="@drawable/icon_link"/>
android:title="@string/activity_settings_link_device_button_title"
android:icon="@drawable/icon_link"/>

<Preference android:key="preference_category_seed"
android:title="@string/activity_settings_show_seed_button_title"
Expand Down
77 changes: 43 additions & 34 deletions src/org/thoughtcrime/securesms/ApplicationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.multidex.MultiDexApplication;

import com.crashlytics.android.Crashlytics;
Expand Down Expand Up @@ -61,8 +62,9 @@
import org.thoughtcrime.securesms.logging.UncaughtExceptionLogger;
import org.thoughtcrime.securesms.loki.BackgroundPollWorker;
import org.thoughtcrime.securesms.loki.LokiAPIDatabase;
import org.thoughtcrime.securesms.loki.LokiGroupChatPoller;
import org.thoughtcrime.securesms.loki.LokiPublicChatManager;
import org.thoughtcrime.securesms.loki.LokiRSSFeedPoller;
import org.thoughtcrime.securesms.loki.LokiUserDatabase;
import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.thoughtcrime.securesms.providers.BlobProvider;
Expand All @@ -85,8 +87,8 @@
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
import org.whispersystems.signalservice.loki.api.LokiAPIDatabaseProtocol;
import org.whispersystems.signalservice.loki.api.LokiGroupChat;
import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI;
import org.whispersystems.signalservice.loki.api.LokiPublicChat;
import org.whispersystems.signalservice.loki.api.LokiPublicChatAPI;
import org.whispersystems.signalservice.loki.api.LokiLongPoller;
import org.whispersystems.signalservice.loki.api.LokiP2PAPI;
import org.whispersystems.signalservice.loki.api.LokiP2PAPIDelegate;
Expand All @@ -98,6 +100,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -132,9 +135,10 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc

// Loki
private LokiLongPoller lokiLongPoller = null;
private LokiGroupChatPoller lokiPublicChatPoller = null;
private LokiRSSFeedPoller lokiNewsFeedPoller = null;
private LokiRSSFeedPoller lokiMessengerUpdatesFeedPoller = null;
private LokiPublicChatManager lokiPublicChatManager = null;
private LokiPublicChatAPI lokiPublicChatAPI = null;
public SignalCommunicationModule communicationModule;
public MixpanelAPI mixpanel;

Expand All @@ -147,7 +151,6 @@ public static ApplicationContext getInstance(Context context) {
@Override
public void onCreate() {
super.onCreate();
LokiGroupChatAPI.Companion.setDebugMode(BuildConfig.DEBUG); // Loki - Set debug mode if needed
startKovenant();
Log.i(TAG, "onCreate()");
initializeSecurityProvider();
Expand Down Expand Up @@ -183,6 +186,8 @@ public void onCreate() {
mixpanel.trackMap(event, properties);
return Unit.INSTANCE;
};
// Loki - Set up public chat manager
lokiPublicChatManager = new LokiPublicChatManager(this);
}

@Override
Expand All @@ -193,6 +198,7 @@ public void onStart(@NonNull LifecycleOwner owner) {
KeyCachingService.onAppForegrounded(this);
// Loki - Start long polling if needed
startLongPollingIfNeeded();
lokiPublicChatManager.startPollersIfNeeded();
setUpStorageAPIIfNeeded();
}

Expand All @@ -204,6 +210,7 @@ public void onStop(@NonNull LifecycleOwner owner) {
MessageNotifier.setVisibleThread(-1);
// Loki - Stop long polling if needed
if (lokiLongPoller != null) { lokiLongPoller.stopIfNeeded(); }
if (lokiPublicChatManager != null) { lokiPublicChatManager.stopPollers(); }
}

@Override
Expand Down Expand Up @@ -243,6 +250,21 @@ public PersistentLogger getPersistentLogger() {
return persistentLogger;
}

public LokiPublicChatManager getLokiPublicChatManager() {
return lokiPublicChatManager;
}

public @Nullable LokiPublicChatAPI getLokiPublicChatAPI() {
if (lokiPublicChatAPI == null && IdentityKeyUtil.hasIdentityKey(this)) {
String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this);
byte[] userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(this).getPrivateKey().serialize();
LokiAPIDatabase apiDatabase = DatabaseFactory.getLokiAPIDatabase(this);
LokiUserDatabase userDatabase = DatabaseFactory.getLokiUserDatabase(this);
lokiPublicChatAPI = new LokiPublicChatAPI(userHexEncodedPublicKey, userPrivateKey, apiDatabase, userDatabase);
}
return lokiPublicChatAPI;
}

private void initializeSecurityProvider() {
try {
Class.forName("org.signal.aesgcmprovider.AesGcmCipher");
Expand Down Expand Up @@ -471,10 +493,6 @@ public void startLongPollingIfNeeded() {
if (lokiLongPoller != null) { lokiLongPoller.startIfNeeded(); }
}

private LokiGroupChat lokiPublicChat() {
return new LokiGroupChat(LokiGroupChatAPI.getPublicChatServerID(), LokiGroupChatAPI.getPublicChatServer(), "Loki Public Chat", true);
}

private LokiRSSFeed lokiNewsFeed() {
return new LokiRSSFeed("loki.network.feed", "https://loki.network/feed/", "Loki News", true);
}
Expand All @@ -483,12 +501,22 @@ private LokiRSSFeed lokiMessengerUpdatesFeed() {
return new LokiRSSFeed("loki.network.messenger-updates.feed", "https://loki.network/category/messenger-updates/feed", "Loki Messenger Updates", false);
}

public void createGroupChatsIfNeeded() {
LokiGroupChat publicChat = lokiPublicChat();
boolean isChatSetUp = TextSecurePreferences.isChatSetUp(this, publicChat.getId());
if (!isChatSetUp || !publicChat.isDeletable()) {
GroupManager.GroupActionResult result = GroupManager.createGroup(publicChat.getId(), this, new HashSet<>(), null, publicChat.getDisplayName(), false);
TextSecurePreferences.markChatSetUp(this, publicChat.getId());
public void createDefaultPublicChatsIfNeeded() {
List<LokiPublicChat> defaultPublicChats = LokiPublicChatAPI.Companion.getDefaultChats(BuildConfig.DEBUG);
for (LokiPublicChat publiChat : defaultPublicChats) {
long threadID = GroupManager.getThreadId(publiChat.getId(), this);
String migrationKey = publiChat.getId() + "_migrated";
boolean isChatMigrated = TextSecurePreferences.getBooleanPreference(this, migrationKey, false);
boolean isChatSetUp = TextSecurePreferences.isChatSetUp(this, publiChat.getId());
if (!isChatSetUp || !publiChat.isDeletable()) {
lokiPublicChatManager.addChat(publiChat.getServer(), publiChat.getChannel(), publiChat.getDisplayName());
TextSecurePreferences.markChatSetUp(this, publiChat.getId());
TextSecurePreferences.setBooleanPreference(this, migrationKey, true);
} else if (threadID > -1 && !isChatMigrated) {
// Migrate the old public chats
DatabaseFactory.getLokiThreadDatabase(this).setPublicChat(publiChat, threadID);
TextSecurePreferences.setBooleanPreference(this, migrationKey, true);
}
}
}

Expand All @@ -505,20 +533,6 @@ public void createRSSFeedsIfNeeded() {
}
}

private void createGroupChatPollersIfNeeded() {
// Only create the group chat pollers if their threads aren't deleted
LokiGroupChat publicChat = lokiPublicChat();
long threadID = GroupManager.getThreadId(publicChat.getId(), this);
if (threadID >= 0 && lokiPublicChatPoller == null) {
lokiPublicChatPoller = new LokiGroupChatPoller(this, publicChat);
// Set up deletion listeners if needed
setUpThreadDeletionListeners(threadID, () -> {
if (lokiPublicChatPoller != null) lokiPublicChatPoller.stop();
lokiPublicChatPoller = null;
});
}
}

private void createRSSFeedPollersIfNeeded() {
// Only create the RSS feed pollers if their threads aren't deleted
LokiRSSFeed lokiNewsFeed = lokiNewsFeed();
Expand Down Expand Up @@ -558,11 +572,6 @@ public void onChange(boolean selfChange) {
this.getContentResolver().registerContentObserver(DatabaseContentProviders.Conversation.getUriForThread(threadID), true, observer);
}

public void startGroupChatPollersIfNeeded() {
createGroupChatPollersIfNeeded();
if (lokiPublicChatPoller != null) lokiPublicChatPoller.startIfNeeded();
}

public void startRSSFeedPollersIfNeeded() {
createRSSFeedPollersIfNeeded();
if (lokiNewsFeedPoller != null) lokiNewsFeedPoller.startIfNeeded();
Expand Down
28 changes: 16 additions & 12 deletions src/org/thoughtcrime/securesms/ConversationListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.TooltipCompat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -44,6 +45,7 @@
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo;
import org.thoughtcrime.securesms.lock.RegistrationLockDialog;
import org.thoughtcrime.securesms.loki.AddPublicChatActivity;
import org.thoughtcrime.securesms.loki.JazzIdenticonDrawable;
import org.thoughtcrime.securesms.notifications.MarkReadReceiver;
import org.thoughtcrime.securesms.notifications.MessageNotifier;
Expand Down Expand Up @@ -82,9 +84,9 @@ protected void onPreCreate() {
dynamicLanguage.onCreate(this);
if (TextSecurePreferences.getLocalNumber(this) != null) {
ApplicationContext application = ApplicationContext.getInstance(this);
application.createGroupChatsIfNeeded();
application.createDefaultPublicChatsIfNeeded();
application.createRSSFeedsIfNeeded();
application.startGroupChatPollersIfNeeded();
application.getLokiPublicChatManager().startPollersIfNeeded();
application.startRSSFeedPollersIfNeeded();
}
}
Expand Down Expand Up @@ -127,18 +129,15 @@ public void onDestroy() {

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return false;
/*
MenuInflater inflater = this.getMenuInflater();
menu.clear();

inflater.inflate(R.menu.text_secure_normal, menu);

menu.findItem(R.id.menu_clear_passphrase).setVisible(!TextSecurePreferences.isPasswordDisabled(this));
// menu.findItem(R.id.menu_clear_passphrase).setVisible(!TextSecurePreferences.isPasswordDisabled(this));

super.onPrepareOptionsMenu(menu);
return true;
*/
}

private void initializeSearchListener() {
Expand Down Expand Up @@ -235,12 +234,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);

switch (item.getItemId()) {
case R.id.menu_new_group: createGroup(); return true;
case R.id.menu_settings: handleDisplaySettings(); return true;
case R.id.menu_clear_passphrase: handleClearPassphrase(); return true;
case R.id.menu_mark_all_read: handleMarkAllRead(); return true;
case R.id.menu_invite: handleInvite(); return true;
case R.id.menu_help: handleHelp(); return true;
// case R.id.menu_new_group: createGroup(); return true;
// case R.id.menu_settings: handleDisplaySettings(); return true;
// case R.id.menu_clear_passphrase: handleClearPassphrase(); return true;
// case R.id.menu_mark_all_read: handleMarkAllRead(); return true;
// case R.id.menu_invite: handleInvite(); return true;
// case R.id.menu_help: handleHelp(); return true;
case R.id.menu_conversation_list_add_public_chat_option: addNewPublicChat(); return true;
}

return false;
Expand Down Expand Up @@ -321,4 +321,8 @@ private void handleHelp() {
Toast.makeText(this, R.string.ConversationListActivity_there_is_no_browser_installed_on_your_device, Toast.LENGTH_LONG).show();
}
}

private void addNewPublicChat() {
startActivity(new Intent(this, AddPublicChatActivity.class));
}
}
2 changes: 1 addition & 1 deletion src/org/thoughtcrime/securesms/ConversationListItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public long getLastSeen() {

private @NonNull CharSequence getTrimmedSnippet(@NonNull CharSequence snippet) {
LokiAPIUtilities.INSTANCE.populateUserHexEncodedPublicKeyCacheIfNeeded(threadId, getContext()); // TODO: Terrible place to do this, but okay for now
snippet = MentionUtilities.highlightMentions(snippet, this.recipient.isGroupRecipient(), getContext());
snippet = MentionUtilities.highlightMentions(snippet, threadId, getContext());
return snippet.length() <= MAX_SNIPPET_LENGTH ? snippet : snippet.subSequence(0, MAX_SNIPPET_LENGTH);
}

Expand Down
Loading

0 comments on commit e8e539c

Please sign in to comment.