Skip to content

Commit

Permalink
Add message sign with openkeychain
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jul 26, 2020
1 parent 538e1b4 commit b1a9d29
Show file tree
Hide file tree
Showing 23 changed files with 2,171 additions and 59 deletions.
18 changes: 12 additions & 6 deletions TMessagesProj/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ configurations {
compile.exclude module: 'support-v4'
}

def okHttpVersion = '4.7.2'
def fcmVersion = '20.2.2'
def okHttpVersion = '4.8.0'
def fcmVersion = '20.2.3'
def crashlyticsVersion = '17.1.1'
def playCoreVersion = '1.7.3'

Expand All @@ -63,6 +63,7 @@ buildscript {

repositories {

jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }

}
Expand Down Expand Up @@ -92,17 +93,19 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'org.osmdroid:osmdroid-android:6.1.6'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8'

implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
implementation "com.squareup.okhttp3:okhttp-dnsoverhttps:$okHttpVersion"
implementation 'dnsjava:dnsjava:3.2.1'
implementation 'dnsjava:dnsjava:3.2.2'
implementation 'org.dizitart:nitrite:3.4.2'

implementation 'cn.hutool:hutool-core:5.3.8'
implementation 'cn.hutool:hutool-crypto:5.3.8'
implementation 'cn.hutool:hutool-core:5.3.9'
implementation 'cn.hutool:hutool-crypto:5.3.9'

implementation 'org.tukaani:xz:1.8'
implementation project(":openpgp-api")


compileOnly files('libs/libv2ray.aar')

Expand Down Expand Up @@ -271,14 +274,17 @@ android {
jniDebuggable false
minifyEnabled true
shrinkResources true
matchingFallbacks = ['debug']
}

release {
initWith releaseNoGcm
matchingFallbacks = ['debug']
}

foss {
initWith release
matchingFallbacks = ['debug']
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ClipDescription;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -48,6 +51,7 @@
import android.text.TextPaint;
import android.text.TextWatcher;
import android.text.style.ImageSpan;
import android.util.Log;
import android.util.Property;
import android.util.TypedValue;
import android.view.ActionMode;
Expand Down Expand Up @@ -88,6 +92,8 @@
import androidx.customview.widget.ExploreByTouchHelper;

import org.jetbrains.annotations.NotNull;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.util.OpenPgpApi;
import org.telegram.messenger.AccountInstance;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.ApplicationLoader;
Expand Down Expand Up @@ -122,19 +128,24 @@
import org.telegram.ui.LaunchActivity;
import org.telegram.ui.StickersActivity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import kotlin.Unit;
import tw.nekomimi.nekogram.NekoConfig;
import tw.nekomimi.nekogram.transtale.TranslateDb;
import tw.nekomimi.nekogram.transtale.Translator;
import tw.nekomimi.nekogram.transtale.TranslatorKt;
import tw.nekomimi.nekogram.utils.AlertUtil;
import tw.nekomimi.nekogram.utils.PGPUtil;

public class ChatActivityEnterView extends FrameLayout implements NotificationCenter.NotificationCenterDelegate, SizeNotifierFrameLayout.SizeNotifierFrameLayoutDelegate, StickersAlert.StickersAlertDelegate {

Expand Down Expand Up @@ -2968,64 +2979,95 @@ public boolean onTouch(View v, MotionEvent event) {
});
sendPopupLayout.setShowedFromBotton(false);

for (int a = 0; a < 3; a++) {
if (a == 2 && (UserObject.isUserSelf(user) || slowModeTimer > 0 && !isInScheduleMode())) {
continue;
}
int num = a;
ActionBarMenuSubItem cell = new ActionBarMenuSubItem(getContext());
if (num == 0) {
cell.setTextAndIcon(LocaleController.getString("Translate", R.string.Translate), R.drawable.ic_translate);
} else if (num == 1) {
if (UserObject.isUserSelf(user)) {
cell.setTextAndIcon(LocaleController.getString("SetReminder", R.string.SetReminder), R.drawable.baseline_date_range_24);
} else {
cell.setTextAndIcon(LocaleController.getString("ScheduleMessage", R.string.ScheduleMessage), R.drawable.baseline_date_range_24);
}
} else if (num == 2) {
cell.setTextAndIcon(LocaleController.getString("SendWithoutSound", R.string.SendWithoutSound), R.drawable.input_notify_off);
}
cell.setMinimumWidth(AndroidUtilities.dp(196));
sendPopupLayout.addView(cell, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 0, 48 * a, 0, 0));
int chatId;
if (chat != null) {
chatId = chat.id;
} else if (user != null) {
chatId = user.id;
} else {
chatId = -1;
}

int chatId;
if (chat != null) {
chatId = chat.id;
} else if (user != null) {
chatId = user.id;
} else {
chatId = -1;
}
int a = 0;

ActionBarMenuSubItem cell = new ActionBarMenuSubItem(getContext());

if (StrUtil.isNotBlank(NekoConfig.openPGPApp)) {

cell.setTextAndIcon(LocaleController.getString("Sign", R.string.Sign), R.drawable.baseline_vpn_key_24);
cell.setOnClickListener(v -> {
if (sendPopupWindow != null && sendPopupWindow.isShowing()) {
sendPopupWindow.dismiss();
}
if (num == 0) {
translateComment(TranslateDb.getChatLanguage(chatId, TranslatorKt.getCode2Locale(NekoConfig.translateInputLang)));
signComment(NekoConfig.openPGPKeyId);

});
cell.setOnLongClickListener(v -> {
if (sendPopupWindow != null && sendPopupWindow.isShowing()) {
sendPopupWindow.dismiss();
}
if (num == 1) {
AlertsCreator.createScheduleDatePickerDialog(parentActivity, parentFragment.getDialogId(), this::sendMessageInternal);
} else if (num == 2) {
sendMessageInternal(false, 0);
signComment(1L);
return true;
});
cell.setMinimumWidth(AndroidUtilities.dp(196));
sendPopupLayout.addView(cell, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 0, 48 * a++, 0, 0));

cell = new ActionBarMenuSubItem(getContext());

}

cell.setTextAndIcon(LocaleController.getString("Translate", R.string.Translate), R.drawable.ic_translate);
cell.setOnClickListener(v -> {
if (sendPopupWindow != null && sendPopupWindow.isShowing()) {
sendPopupWindow.dismiss();
}
translateComment(TranslateDb.getChatLanguage(chatId, TranslatorKt.getCode2Locale(NekoConfig.translateInputLang)));
});
ActionBarMenuSubItem finalCell = cell;
cell.setOnLongClickListener(v -> {
Translator.showTargetLangSelect(finalCell, true, (locale) -> {
if (sendPopupWindow != null && sendPopupWindow.isShowing()) {
sendPopupWindow.dismiss();
}
translateComment(locale);
TranslateDb.saveChatLanguage(chatId, locale);
return Unit.INSTANCE;
});
cell.setOnLongClickListener(v -> {
if (num == 0) {
Translator.showTargetLangSelect(cell, true, (locale) -> {
if (sendPopupWindow != null && sendPopupWindow.isShowing()) {
sendPopupWindow.dismiss();
}
translateComment(locale);
TranslateDb.saveChatLanguage(chatId, locale);
return Unit.INSTANCE;
});
return true;
return true;
});
cell.setMinimumWidth(AndroidUtilities.dp(196));
sendPopupLayout.addView(cell, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 0, 48 * a++, 0, 0));

if (!UserObject.isUserSelf(user) && (slowModeTimer == 0 || !isInScheduleMode())) {

cell = new ActionBarMenuSubItem(getContext());
if (UserObject.isUserSelf(user)) {
cell.setTextAndIcon(LocaleController.getString("SetReminder", R.string.SetReminder), R.drawable.baseline_date_range_24);
} else {
cell.setTextAndIcon(LocaleController.getString("ScheduleMessage", R.string.ScheduleMessage), R.drawable.baseline_date_range_24);
}
cell.setOnClickListener(v -> {
if (sendPopupWindow != null && sendPopupWindow.isShowing()) {
sendPopupWindow.dismiss();
}
return false;
AlertsCreator.createScheduleDatePickerDialog(parentActivity, parentFragment.getDialogId(), this::sendMessageInternal);
});
cell.setMinimumWidth(AndroidUtilities.dp(196));
sendPopupLayout.addView(cell, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 0, 48 * a++, 0, 0));

}

cell = new ActionBarMenuSubItem(getContext());
cell.setTextAndIcon(LocaleController.getString("SendWithoutSound", R.string.SendWithoutSound), R.drawable.input_notify_off);
cell.setOnClickListener(v -> {
if (sendPopupWindow != null && sendPopupWindow.isShowing()) {
sendPopupWindow.dismiss();
}
AlertsCreator.createScheduleDatePickerDialog(parentActivity, parentFragment.getDialogId(), this::sendMessageInternal);
});
cell.setMinimumWidth(AndroidUtilities.dp(196));
sendPopupLayout.addView(cell, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT, 0, 48 * a++, 0, 0));

sendPopupWindow = new ActionBarPopupWindow(sendPopupLayout, LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT) {
@Override
public void dismiss() {
Expand Down Expand Up @@ -3065,6 +3107,71 @@ public void dismiss() {
return false;
}

private void signComment(long signKeyId) {

if (parentActivity instanceof LaunchActivity) {

((LaunchActivity) parentActivity).callbacks.put(115, result -> {

long keyId = result.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, 0L);
if (signKeyId < 1L) NekoConfig.setOpenPGPKeyId(keyId);

signComment(keyId);

return Unit.INSTANCE;

});

}

Intent intent = new Intent(OpenPgpApi.ACTION_CLEARTEXT_SIGN);

if (signKeyId < 0L) intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, signKeyId);

ByteArrayInputStream is = IoUtil.toUtf8Stream(messageEditText.getText().toString());
ByteArrayOutputStream os = new ByteArrayOutputStream();

PGPUtil.post(() -> PGPUtil.api.executeApiAsync(intent, is, os, result -> {

switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {

case OpenPgpApi.RESULT_CODE_SUCCESS: {

String str = StrUtil.utf8Str(os.toByteArray());

if (StrUtil.isNotBlank(str)) messageEditText.setText(str);

break;

}

case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: {

PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
try {
parentActivity.startIntentSenderFromChild(parentActivity, pi.getIntentSender(),
115, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.e(OpenPgpApi.TAG, "SendIntentException", e);
}
break;
}
case OpenPgpApi.RESULT_CODE_ERROR: {
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
if (error.getMessage().contains("not found") && signKeyId < 0L) {
NekoConfig.setOpenPGPKeyId(0L);
signComment(0L);
} else {
AlertUtil.showToast(error.getMessage());
}
break;
}
}

}));

}

private void translateComment(Locale target) {

TranslateDb db = TranslateDb.forLocale(target);
Expand Down
13 changes: 13 additions & 0 deletions TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import kotlin.Unit;
import tw.nekomimi.nekogram.NekoConfig;
import tw.nekomimi.nekogram.NekoXConfig;
import tw.nekomimi.nekogram.NekoXSettingActivity;
Expand Down Expand Up @@ -2891,6 +2893,8 @@ public ActionBarLayout getRightActionBarLayout() {
return rightActionBarLayout;
}

public HashMap<Integer, Function<Intent, Unit>> callbacks = new HashMap<>();

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (SharedConfig.passcodeHash.length() != 0 && SharedConfig.lastPauseTime != 0) {
Expand All @@ -2901,6 +2905,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
UserConfig.getInstance(currentAccount).saveConfig(false);
}
super.onActivityResult(requestCode, resultCode, data);

if (callbacks.containsKey(requestCode)) {

callbacks.remove(requestCode).apply(data);

return;

}

ThemeEditorView editorView = ThemeEditorView.getInstance();
if (editorView != null) {
editorView.onActivityResult(requestCode, resultCode, data);
Expand Down
Loading

0 comments on commit b1a9d29

Please sign in to comment.