parent
54dbffaf30
commit
e6c16cf28d
@ -0,0 +1,70 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<android.support.v7.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
android:theme="@style/TextSecure.LightActionBar"
|
||||
android:background="@color/signal_primary"
|
||||
android:elevation="4dp"/>
|
||||
|
||||
<ScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="12dp">
|
||||
|
||||
<org.thoughtcrime.securesms.components.emoji.EmojiTextView
|
||||
android:id="@+id/name_edit_display_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:textSize="20sp"
|
||||
tools:text="Peter Parker"/>
|
||||
|
||||
<org.thoughtcrime.securesms.components.emoji.EmojiEditText
|
||||
android:id="@+id/name_edit_prefix"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/ContactNameEditActivity_prefix"/>
|
||||
|
||||
<org.thoughtcrime.securesms.components.emoji.EmojiEditText
|
||||
android:id="@+id/name_edit_given_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/ContactNameEditActivity_given_name"/>
|
||||
|
||||
<org.thoughtcrime.securesms.components.emoji.EmojiEditText
|
||||
android:id="@+id/name_edit_middle_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/ContactNameEditActivity_middle_name"/>
|
||||
|
||||
<org.thoughtcrime.securesms.components.emoji.EmojiEditText
|
||||
android:id="@+id/name_edit_family_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/ContactNameEditActivity_family_name"/>
|
||||
|
||||
<org.thoughtcrime.securesms.components.emoji.EmojiEditText
|
||||
android:id="@+id/name_edit_suffix"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/ContactNameEditActivity_suffix"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
||||
|
||||
</LinearLayout>
|
@ -0,0 +1,156 @@
|
||||
package org.thoughtcrime.securesms.contactshare;
|
||||
|
||||
import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
||||
import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
|
||||
import org.thoughtcrime.securesms.util.DynamicTheme;
|
||||
|
||||
import static org.thoughtcrime.securesms.contactshare.Contact.*;
|
||||
|
||||
public class ContactNameEditActivity extends PassphraseRequiredActionBarActivity {
|
||||
|
||||
public static final String KEY_NAME = "name";
|
||||
public static final String KEY_CONTACT_INDEX = "contact_index";
|
||||
|
||||
private final DynamicTheme dynamicTheme = new DynamicNoActionBarTheme();
|
||||
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
||||
|
||||
private TextView displayNameView;
|
||||
private ContactNameEditViewModel viewModel;
|
||||
|
||||
static Intent getIntent(@NonNull Context context, @NonNull Name name, int contactPosition) {
|
||||
Intent intent = new Intent(context, ContactNameEditActivity.class);
|
||||
intent.putExtra(KEY_NAME, name);
|
||||
intent.putExtra(KEY_CONTACT_INDEX, contactPosition);
|
||||
return intent;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreCreate() {
|
||||
dynamicTheme.onCreate(this);
|
||||
dynamicLanguage.onCreate(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState, boolean ready) {
|
||||
super.onCreate(savedInstanceState, ready);
|
||||
|
||||
if (getIntent() == null) {
|
||||
throw new IllegalStateException("You must supply extras to this activity. Please use the #getIntent() method.");
|
||||
}
|
||||
|
||||
Name name = getIntent().getParcelableExtra(KEY_NAME);
|
||||
if (name == null) {
|
||||
throw new IllegalStateException("You must supply a name to this activity. Please use the #getIntent() method.");
|
||||
}
|
||||
|
||||
setContentView(R.layout.activity_contact_name_edit);
|
||||
|
||||
initializeToolbar();
|
||||
initializeViews(name);
|
||||
|
||||
viewModel = ViewModelProviders.of(this).get(ContactNameEditViewModel.class);
|
||||
viewModel.setName(name);
|
||||
viewModel.getDisplayName().observe(this, displayNameView::setText);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
dynamicTheme.onResume(this);
|
||||
dynamicLanguage.onResume(this);
|
||||
}
|
||||
|
||||
private void initializeToolbar() {
|
||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
toolbar.setTitle("");
|
||||
toolbar.setNavigationIcon(R.drawable.ic_check_white_24dp);
|
||||
toolbar.setNavigationOnClickListener(v -> {
|
||||
Intent resultIntent = new Intent();
|
||||
resultIntent.putExtra(KEY_NAME, viewModel.getName());
|
||||
resultIntent.putExtra(KEY_CONTACT_INDEX, getIntent().getIntExtra(KEY_CONTACT_INDEX, -1));
|
||||
setResult(RESULT_OK, resultIntent);
|
||||
finish();
|
||||
});
|
||||
}
|
||||
|
||||
private void initializeViews(@NonNull Name name) {
|
||||
displayNameView = findViewById(R.id.name_edit_display_name);
|
||||
|
||||
TextView givenName = findViewById(R.id.name_edit_given_name);
|
||||
TextView familyName = findViewById(R.id.name_edit_family_name);
|
||||
TextView middleName = findViewById(R.id.name_edit_middle_name);
|
||||
TextView prefix = findViewById(R.id.name_edit_prefix);
|
||||
TextView suffix = findViewById(R.id.name_edit_suffix);
|
||||
|
||||
givenName.setText(name.getGivenName());
|
||||
familyName.setText(name.getFamilyName());
|
||||
middleName.setText(name.getMiddleName());
|
||||
prefix.setText(name.getPrefix());
|
||||
suffix.setText(name.getSuffix());
|
||||
|
||||
givenName.addTextChangedListener(new SimpleTextWatcher() {
|
||||
@Override
|
||||
void onTextChanged(String text) {
|
||||
viewModel.updateGivenName(text);
|
||||
}
|
||||
});
|
||||
|
||||
familyName.addTextChangedListener(new SimpleTextWatcher() {
|
||||
@Override
|
||||
void onTextChanged(String text) {
|
||||
viewModel.updateFamilyName(text);
|
||||
}
|
||||
});
|
||||
|
||||
middleName.addTextChangedListener(new SimpleTextWatcher() {
|
||||
@Override
|
||||
void onTextChanged(String text) {
|
||||
viewModel.updateMiddleName(text);
|
||||
}
|
||||
});
|
||||
|
||||
prefix.addTextChangedListener(new SimpleTextWatcher() {
|
||||
@Override
|
||||
void onTextChanged(String text) {
|
||||
viewModel.updatePrefix(text);
|
||||
}
|
||||
});
|
||||
|
||||
suffix.addTextChangedListener(new SimpleTextWatcher() {
|
||||
@Override
|
||||
void onTextChanged(String text) {
|
||||
viewModel.updateSuffix(text);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static abstract class SimpleTextWatcher implements TextWatcher {
|
||||
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
onTextChanged(s.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) { }
|
||||
|
||||
abstract void onTextChanged(String text);
|
||||
}
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
package org.thoughtcrime.securesms.contactshare;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.MutableLiveData;
|
||||
import android.arch.lifecycle.ViewModel;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import static org.thoughtcrime.securesms.contactshare.Contact.*;
|
||||
|
||||
public class ContactNameEditViewModel extends ViewModel {
|
||||
|
||||
private final MutableLiveData<String> displayName;
|
||||
|
||||
private String givenName;
|
||||
private String familyName;
|
||||
private String middleName;
|
||||
private String prefix;
|
||||
private String suffix;
|
||||
|
||||
public ContactNameEditViewModel() {
|
||||
this.displayName = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
void setName(@NonNull Name name) {
|
||||
givenName = name.getGivenName();
|
||||
familyName = name.getFamilyName();
|
||||
middleName = name.getMiddleName();
|
||||
prefix = name.getPrefix();
|
||||
suffix = name.getSuffix();
|
||||
|
||||
displayName.postValue(buildDisplayName());
|
||||
}
|
||||
|
||||
Name getName() {
|
||||
return new Name(displayName.getValue(), givenName, familyName, prefix, suffix, middleName);
|
||||
}
|
||||
|
||||
LiveData<String> getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
void updateGivenName(@NonNull String givenName) {
|
||||
this.givenName = givenName;
|
||||
displayName.postValue(buildDisplayName());
|
||||
}
|
||||
|
||||
void updateFamilyName(@NonNull String familyName) {
|
||||
this.familyName = familyName;
|
||||
displayName.postValue(buildDisplayName());
|
||||
}
|
||||
|
||||
void updatePrefix(@NonNull String prefix) {
|
||||
this.prefix = prefix;
|
||||
displayName.postValue(buildDisplayName());
|
||||
}
|
||||
|
||||
void updateSuffix(@NonNull String suffix) {
|
||||
this.suffix = suffix;
|
||||
displayName.postValue(buildDisplayName());
|
||||
}
|
||||
|
||||
void updateMiddleName(@NonNull String middleName) {
|
||||
this.middleName = middleName;
|
||||
displayName.postValue(buildDisplayName());
|
||||
}
|
||||
|
||||
private String buildDisplayName() {
|
||||
boolean isCJKV = isCJKV(givenName) && isCJKV(middleName) && isCJKV(familyName) && isCJKV(prefix) && isCJKV(suffix);
|
||||
if (isCJKV) {
|
||||
return joinString(familyName, givenName, prefix, suffix, middleName);
|
||||
}
|
||||
return joinString(prefix, givenName, middleName, familyName, suffix);
|
||||
}
|
||||
|
||||
private String joinString(String... values) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
for (String value : values) {
|
||||
if (!TextUtils.isEmpty(value)) {
|
||||
builder.append(value).append(' ');
|
||||
}
|
||||
}
|
||||
|
||||
return builder.toString().trim();
|
||||
}
|
||||
|
||||
private boolean isCJKV(@Nullable String value) {
|
||||
if (TextUtils.isEmpty(value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int offset = 0; offset < value.length(); ) {
|
||||
int codepoint = Character.codePointAt(value, offset);
|
||||
|
||||
if (!isCodepointCJKV(codepoint)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
offset += Character.charCount(codepoint);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isCodepointCJKV(int codepoint) {
|
||||
if (codepoint == (int)' ') return true;
|
||||
|
||||
Character.UnicodeBlock block = Character.UnicodeBlock.of(codepoint);
|
||||
|
||||
boolean isCJKV = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(block) ||
|
||||
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A.equals(block) ||
|
||||
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B.equals(block) ||
|
||||
Character.UnicodeBlock.CJK_COMPATIBILITY.equals(block) ||
|
||||
Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS.equals(block) ||
|
||||
Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS.equals(block) ||
|
||||
Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT.equals(block) ||
|
||||
Character.UnicodeBlock.CJK_RADICALS_SUPPLEMENT.equals(block) ||
|
||||
Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION.equals(block) ||
|
||||
Character.UnicodeBlock.ENCLOSED_CJK_LETTERS_AND_MONTHS.equals(block) ||
|
||||
Character.UnicodeBlock.KANGXI_RADICALS.equals(block) ||
|
||||
Character.UnicodeBlock.IDEOGRAPHIC_DESCRIPTION_CHARACTERS.equals(block) ||
|
||||
Character.UnicodeBlock.HIRAGANA.equals(block) ||
|
||||
Character.UnicodeBlock.KATAKANA.equals(block) ||
|
||||
Character.UnicodeBlock.KATAKANA_PHONETIC_EXTENSIONS.equals(block) ||
|
||||
Character.UnicodeBlock.HANGUL_JAMO.equals(block) ||
|
||||
Character.UnicodeBlock.HANGUL_COMPATIBILITY_JAMO.equals(block) ||
|
||||
Character.UnicodeBlock.HANGUL_SYLLABLES.equals(block);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 19) {
|
||||
isCJKV |= Character.isIdeographic(codepoint);
|
||||
}
|
||||
|
||||
return isCJKV;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue