NekoX/TMessagesProj/src/main/java/org/telegram/ui/IdenticonActivity.java

193 lines
7.7 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2015-10-29 18:10:07 +01:00
* This is the source code of Telegram for Android v. 3.x.x.
2013-10-25 17:19:00 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2013-2016.
2013-10-25 17:19:00 +02:00
*/
package org.telegram.ui;
import android.content.Context;
import android.os.Bundle;
2016-03-06 02:49:31 +01:00
import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.TypedValue;
import android.view.Gravity;
2014-11-19 16:17:24 +01:00
import android.view.MotionEvent;
2013-10-25 17:19:00 +02:00
import android.view.Surface;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
2016-03-06 02:49:31 +01:00
import android.widget.FrameLayout;
2014-11-10 12:05:22 +01:00
import android.widget.ImageView;
2013-10-25 17:19:00 +02:00
import android.widget.LinearLayout;
import android.widget.TextView;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2016-03-06 02:49:31 +01:00
import org.telegram.messenger.FileLog;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.ApplicationLoader;
2016-03-06 02:49:31 +01:00
import org.telegram.messenger.Utilities;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.TLRPC;
import org.telegram.messenger.MessagesController;
2013-10-25 17:19:00 +02:00
import org.telegram.messenger.R;
2014-11-13 21:10:14 +01:00
import org.telegram.ui.ActionBar.ActionBar;
import org.telegram.ui.ActionBar.BaseFragment;
import org.telegram.ui.Components.IdenticonDrawable;
2016-03-06 02:49:31 +01:00
import org.telegram.ui.Components.LayoutHelper;
2016-04-22 15:49:00 +02:00
import org.telegram.ui.ActionBar.Theme;
2016-03-06 02:49:31 +01:00
import org.telegram.ui.Components.URLSpanReplacement;
2013-10-25 17:19:00 +02:00
public class IdenticonActivity extends BaseFragment {
2016-03-06 02:49:31 +01:00
2013-10-25 17:19:00 +02:00
private int chat_id;
2016-03-06 02:49:31 +01:00
private static class LinkMovementMethodMy extends LinkMovementMethod {
@Override
public boolean onTouchEvent(@NonNull TextView widget, @NonNull Spannable buffer, @NonNull MotionEvent event) {
try {
return super.onTouchEvent(widget, buffer, event);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
return false;
}
}
public IdenticonActivity(Bundle args) {
super(args);
}
2013-10-25 17:19:00 +02:00
@Override
public boolean onFragmentCreate() {
chat_id = getArguments().getInt("chat_id");
return super.onFragmentCreate();
}
@Override
2015-07-22 20:56:37 +02:00
public View createView(Context context) {
2015-04-09 20:00:14 +02:00
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
actionBar.setAllowOverlayTitle(true);
actionBar.setTitle(LocaleController.getString("EncryptionKey", R.string.EncryptionKey));
2013-10-25 17:19:00 +02:00
2015-04-09 20:00:14 +02:00
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
}
2013-10-25 17:19:00 +02:00
}
2015-04-09 20:00:14 +02:00
});
2014-11-19 16:17:24 +01:00
2016-03-06 02:49:31 +01:00
fragmentView = new LinearLayout(context);
LinearLayout linearLayout = (LinearLayout) fragmentView;
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setWeightSum(100);
linearLayout.setBackgroundColor(0xfff0f0f0);
fragmentView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setPadding(AndroidUtilities.dp(20), AndroidUtilities.dp(20), AndroidUtilities.dp(20), AndroidUtilities.dp(20));
linearLayout.addView(frameLayout, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, 50.0f));
ImageView identiconView = new ImageView(context);
identiconView.setScaleType(ImageView.ScaleType.FIT_XY);
frameLayout.addView(identiconView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
frameLayout = new FrameLayout(context);
frameLayout.setBackgroundColor(0xffffffff);
frameLayout.setPadding(AndroidUtilities.dp(10), 0, AndroidUtilities.dp(10), AndroidUtilities.dp(10));
linearLayout.addView(frameLayout, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, 50.0f));
TextView textView = new TextView(context);
textView.setTextColor(0xff7f7f7f);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setLinksClickable(true);
textView.setClickable(true);
textView.setMovementMethod(new LinkMovementMethodMy());
//textView.setAutoLinkMask(Linkify.WEB_URLS);
2016-04-22 15:49:00 +02:00
textView.setLinkTextColor(Theme.MSG_LINK_TEXT_COLOR);
2016-03-06 02:49:31 +01:00
textView.setGravity(Gravity.CENTER);
frameLayout.addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
2015-04-09 20:00:14 +02:00
TLRPC.EncryptedChat encryptedChat = MessagesController.getInstance().getEncryptedChat(chat_id);
if (encryptedChat != null) {
IdenticonDrawable drawable = new IdenticonDrawable();
identiconView.setImageDrawable(drawable);
drawable.setEncryptedChat(encryptedChat);
TLRPC.User user = MessagesController.getInstance().getUser(encryptedChat.user_id);
2016-03-06 02:49:31 +01:00
SpannableStringBuilder hash = new SpannableStringBuilder();
if (encryptedChat.key_hash.length > 16) {
String hex = Utilities.bytesToHex(encryptedChat.key_hash);
for (int a = 0; a < 32; a++) {
if (a != 0) {
if (a % 8 == 0) {
hash.append('\n');
} else if (a % 4 == 0) {
hash.append(' ');
}
}
hash.append(hex.substring(a * 2, a * 2 + 2));
hash.append(' ');
}
hash.append("\n\n");
2015-04-09 20:00:14 +02:00
}
2016-03-06 02:49:31 +01:00
hash.append(AndroidUtilities.replaceTags(LocaleController.formatString("EncryptionKeyDescription", R.string.EncryptionKeyDescription, user.first_name, user.first_name)));
final String url = "telegram.org";
int index = hash.toString().indexOf(url);
if (index != -1) {
hash.setSpan(new URLSpanReplacement(LocaleController.getString("EncryptionKeyLink", R.string.EncryptionKeyLink)), index, index + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setText(hash);
}
2015-04-09 20:00:14 +02:00
2013-10-25 17:19:00 +02:00
return fragmentView;
}
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
super.onConfigurationChanged(newConfig);
fixLayout();
}
@Override
public void onResume() {
super.onResume();
2013-10-25 17:19:00 +02:00
fixLayout();
}
private void fixLayout() {
ViewTreeObserver obs = fragmentView.getViewTreeObserver();
obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
2015-06-29 19:12:11 +02:00
if (fragmentView == null) {
return true;
}
2015-06-29 19:12:11 +02:00
fragmentView.getViewTreeObserver().removeOnPreDrawListener(this);
2016-03-06 02:49:31 +01:00
LinearLayout layout = (LinearLayout) fragmentView;
WindowManager manager = (WindowManager) ApplicationLoader.applicationContext.getSystemService(Context.WINDOW_SERVICE);
int rotation = manager.getDefaultDisplay().getRotation();
if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) {
layout.setOrientation(LinearLayout.HORIZONTAL);
} else {
layout.setOrientation(LinearLayout.VERTICAL);
2013-10-25 17:19:00 +02:00
}
fragmentView.setPadding(fragmentView.getPaddingLeft(), 0, fragmentView.getPaddingRight(), fragmentView.getPaddingBottom());
2015-06-29 19:12:11 +02:00
return true;
}
});
2013-10-25 17:19:00 +02:00
}
}