feat(nfc): 新增老人端 NFC 联系人识别
This commit is contained in:
1 parent
206d4b702b
commit
650657fcc7
8 files changed
+345
-1
No files matched your search
@@ -4,6 +4,9 @@
|
||||
package="com.ttstd.dialer"
|
||||
android:sharedUserId="android.uid.system">
|
||||
|
||||
<uses-permission android:name="android.permission.NFC" />
|
||||
<uses-feature android:name="android.hardware.nfc" android:required="false" />
|
||||
|
||||
<queries>
|
||||
<package android:name="com.ttstd.tts.engine" />
|
||||
<intent><action android:name="android.intent.action.TTS_SERVICE" /></intent>
|
||||
@@ -123,6 +126,43 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".activity.nfc.NfcContactActivity"
|
||||
android:exported="false"
|
||||
android:launchMode="singleTop" />
|
||||
<activity-alias
|
||||
android:name=".activity.nfc.NfcContactLegacyDispatch"
|
||||
android:enabled="@bool/nfc_legacy_dispatch"
|
||||
android:exported="true"
|
||||
android:targetActivity=".activity.nfc.NfcContactActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:mimeType="application/vnd.ttstd.dialer.contact" />
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:scheme="elderlyassistant" android:host="ttstd.com" />
|
||||
</intent-filter>
|
||||
</activity-alias>
|
||||
<activity-alias
|
||||
android:name=".activity.nfc.NfcContactSystemDispatch"
|
||||
android:enabled="@bool/nfc_system_dispatch"
|
||||
android:exported="true"
|
||||
android:permission="android.permission.DISPATCH_NFC_MESSAGE"
|
||||
android:targetActivity=".activity.nfc.NfcContactActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:mimeType="application/vnd.ttstd.dialer.contact" />
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:scheme="elderlyassistant" android:host="ttstd.com" />
|
||||
</intent-filter>
|
||||
</activity-alias>
|
||||
<activity
|
||||
android:name=".activity.contact.add.ContactAddActivity"
|
||||
android:launchMode="singleTask"
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
package com.ttstd.dialer.activity.nfc;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.nfc.NdefMessage;
|
||||
import android.nfc.NfcAdapter;
|
||||
import android.nfc.tech.Ndef;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.ttstd.dialer.R;
|
||||
import com.ttstd.dialer.accessibility.DialerAccessibilityService;
|
||||
import com.ttstd.dialer.base.mvvm.BaseMvvmActivity;
|
||||
import com.ttstd.dialer.databinding.ActivityNfcContactBinding;
|
||||
import com.ttstd.dialer.db.contact.ContactInfo;
|
||||
|
||||
public class NfcContactActivity extends BaseMvvmActivity<NfcContactViewModel, ActivityNfcContactBinding> {
|
||||
public static final String ACTION_SCAN_CONTACT = "com.ttstd.dialer.action.SCAN_NFC_CONTACT";
|
||||
private NfcAdapter scanAdapter;
|
||||
private int scanGeneration;
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return R.layout.activity_nfc_contact;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initDataBinding() {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
mViewDataBinding.nfcCancel.setOnClickListener(view -> finish());
|
||||
if (!ACTION_SCAN_CONTACT.equals(getIntent().getAction())) read(getIntent());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void observeViewModel() {
|
||||
mViewModel.getAction().observe(this, this::render);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
setIntent(intent);
|
||||
if (!ACTION_SCAN_CONTACT.equals(intent.getAction())) read(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (!ACTION_SCAN_CONTACT.equals(getIntent().getAction())) return;
|
||||
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
|
||||
if (adapter == null || !adapter.isEnabled()) {
|
||||
mViewDataBinding.nfcContact.setText(adapter == null ? "此设备不支持 NFC" : "请在系统设置中开启 NFC");
|
||||
return;
|
||||
}
|
||||
if (mViewModel.getAction().getValue() == null) {
|
||||
mViewDataBinding.nfcContact.setText("请将 NFC 联系人标签贴近手机");
|
||||
}
|
||||
int generation = ++scanGeneration;
|
||||
adapter.enableReaderMode(this, tag -> {
|
||||
Ndef ndef = Ndef.get(tag);
|
||||
NdefMessage message = ndef == null ? null : ndef.getCachedNdefMessage();
|
||||
runOnUiThread(() -> {
|
||||
if (scanGeneration == generation) mViewModel.read(new NdefMessage[]{message});
|
||||
});
|
||||
}, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B
|
||||
| NfcAdapter.FLAG_READER_NFC_F | NfcAdapter.FLAG_READER_NFC_V, null);
|
||||
scanAdapter = adapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
scanGeneration++;
|
||||
if (scanAdapter != null) {
|
||||
scanAdapter.disableReaderMode(this);
|
||||
scanAdapter = null;
|
||||
}
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
private void read(Intent intent) {
|
||||
if (!NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
mViewModel.read(intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES));
|
||||
} catch (RuntimeException e) {
|
||||
mViewModel.read(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void render(NfcContactViewModel.ContactAction action) {
|
||||
mViewDataBinding.nfcConfirm.setOnClickListener(null);
|
||||
mViewDataBinding.nfcConfirm.setEnabled(false);
|
||||
if (action == null) {
|
||||
mViewDataBinding.nfcContact.setText("无法读取 NFC 联系人");
|
||||
return;
|
||||
}
|
||||
if (action.operation >= 5) {
|
||||
mViewDataBinding.nfcContact.setText("此标签使用 QQ 通话,老人端暂不支持");
|
||||
return;
|
||||
}
|
||||
String[] labels = {"打开拨号盘", "打开短信", "微信语音", "微信视频"};
|
||||
String target = action.operation <= 2 ? action.phone : action.wechat;
|
||||
mViewDataBinding.nfcContact.setText(action.name + "\n" + target);
|
||||
mViewDataBinding.nfcConfirm.setText(labels[action.operation - 1]);
|
||||
mViewDataBinding.nfcConfirm.setEnabled(true);
|
||||
mViewDataBinding.nfcConfirm.setOnClickListener(view -> launch(action));
|
||||
}
|
||||
|
||||
private void launch(NfcContactViewModel.ContactAction action) {
|
||||
try {
|
||||
if (action.operation <= 2) {
|
||||
boolean call = action.operation == 1;
|
||||
startActivity(new Intent(call ? Intent.ACTION_DIAL : Intent.ACTION_SENDTO,
|
||||
Uri.fromParts(call ? "tel" : "smsto", action.phone, null)));
|
||||
} else {
|
||||
if (!DialerAccessibilityService.isConnected()) {
|
||||
Toast.makeText(this, "请先开启老人端无障碍服务", Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
ContactInfo contact = new ContactInfo();
|
||||
contact.setName(action.wechat);
|
||||
startService(new Intent(this, DialerAccessibilityService.class)
|
||||
.putExtra("ContactInfo", contact)
|
||||
.putExtra(DialerAccessibilityService.EXTRA_CALL_TYPE,
|
||||
action.operation == 3 ? DialerAccessibilityService.ACTION_AUDIO
|
||||
: DialerAccessibilityService.ACTION_VIDEO));
|
||||
}
|
||||
finish();
|
||||
} catch (RuntimeException e) {
|
||||
Toast.makeText(this, "无法打开此操作", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package com.ttstd.dialer.activity.nfc;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.nfc.NdefMessage;
|
||||
import android.nfc.NdefRecord;
|
||||
import android.os.Parcelable;
|
||||
import android.util.Base64;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import com.ttstd.dialer.base.mvvm.BaseViewModel;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
public class NfcContactViewModel extends BaseViewModel {
|
||||
static final String MIME = "application/vnd.ttstd.dialer.contact";
|
||||
private final MutableLiveData<ContactAction> action = new MutableLiveData<>();
|
||||
|
||||
LiveData<ContactAction> getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
void read(Parcelable[] messages) {
|
||||
action.setValue(readAction(messages));
|
||||
}
|
||||
|
||||
private ContactAction readAction(Parcelable[] messages) {
|
||||
if (messages == null) return null;
|
||||
for (Parcelable message : messages) {
|
||||
if (!(message instanceof NdefMessage)) continue;
|
||||
for (NdefRecord record : ((NdefMessage) message).getRecords()) {
|
||||
try {
|
||||
ContactAction contact = parse(record);
|
||||
if (contact != null) return contact;
|
||||
} catch (Exception ignored) {
|
||||
// A malformed or unrelated record may precede the contact record.
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private ContactAction parse(NdefRecord record) throws Exception {
|
||||
byte[] payload = record.getPayload();
|
||||
if (MIME.equals(record.toMimeType())) {
|
||||
return fromJson(new String(payload, StandardCharsets.UTF_8), 1, true);
|
||||
}
|
||||
if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN
|
||||
&& Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) {
|
||||
if (payload.length == 0) return null;
|
||||
int offset = 1 + (payload[0] & 0x3f);
|
||||
if (offset >= payload.length) return null;
|
||||
return fromJson(new String(payload, offset, payload.length - offset,
|
||||
(payload[0] & 0x80) == 0 ? StandardCharsets.UTF_8 : StandardCharsets.UTF_16),
|
||||
3, false);
|
||||
}
|
||||
Uri uri = record.toUri();
|
||||
if (uri == null || !"elderlyassistant".equals(uri.getScheme())
|
||||
|| !"ttstd.com".equals(uri.getHost())) return null;
|
||||
String detail = uri.getQueryParameter("detail");
|
||||
if (detail == null) return null;
|
||||
if ("/encrypted".equals(uri.getPath())) {
|
||||
return fromJson(decryptLegacy(detail), 1, false);
|
||||
}
|
||||
return "/contactInfo".equals(uri.getPath()) ? fromJson(detail, 1, false) : null;
|
||||
}
|
||||
|
||||
private String decryptLegacy(String detail) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
// Match ElderlyAssistant's legacy card format.
|
||||
cipher.init(Cipher.DECRYPT_MODE,
|
||||
new SecretKeySpec("YourSecretKey123".getBytes(StandardCharsets.US_ASCII), "AES"),
|
||||
new IvParameterSpec("RandomInitVector".getBytes(StandardCharsets.US_ASCII)));
|
||||
return new String(cipher.doFinal(Base64.decode(detail.trim(), Base64.DEFAULT)),
|
||||
StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private ContactAction fromJson(String payload, int defaultOperation, boolean current)
|
||||
throws Exception {
|
||||
JSONObject data = new JSONObject(payload);
|
||||
if ((current && data.optInt("version") != 1) || !(data.opt("name") instanceof String)) {
|
||||
return null;
|
||||
}
|
||||
String name = data.optString("name", "").trim();
|
||||
String phone = data.optString("phone", "").replaceAll("[ ()-]", "");
|
||||
int operation = data.optInt("operation", defaultOperation);
|
||||
if (name.isEmpty() || operation < 1 || operation > 6) return null;
|
||||
if (operation <= 2 && !phone.matches("[+]?[0-9]{1,32}")) return null;
|
||||
String wechat = data.isNull("wxid") ? "" : data.optString("wxid").trim();
|
||||
return new ContactAction(name, phone, wechat.isEmpty() ? name : wechat, operation);
|
||||
}
|
||||
|
||||
static final class ContactAction {
|
||||
final String name;
|
||||
final String phone;
|
||||
final String wechat;
|
||||
final int operation;
|
||||
|
||||
ContactAction(String name, String phone, String wechat, int operation) {
|
||||
this.name = name;
|
||||
this.phone = phone;
|
||||
this.wechat = wechat;
|
||||
this.operation = operation;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import com.ttstd.dialer.service.MyVoiceInteractionService;
|
||||
import com.ttstd.dialer.view.SwitchButton;
|
||||
import com.ttstd.dialer.R;
|
||||
import com.ttstd.dialer.activity.main.MainActivity;
|
||||
import com.ttstd.dialer.activity.nfc.NfcContactActivity;
|
||||
import com.ttstd.dialer.base.mvvm.BaseMvvmActivity;
|
||||
import com.ttstd.dialer.databinding.ActivitySettingsUtilsBinding;
|
||||
import com.ttstd.dialer.mdm.DeviceManagerService;
|
||||
@@ -135,6 +136,11 @@ public class SettingsUtilsActivity extends BaseMvvmActivity<SettingsUtilsViewMod
|
||||
}
|
||||
|
||||
public class BtnClick {
|
||||
public void scanNfcContact(View view) {
|
||||
startActivity(new Intent(SettingsUtilsActivity.this, NfcContactActivity.class)
|
||||
.setAction(NfcContactActivity.ACTION_SCAN_CONTACT));
|
||||
}
|
||||
|
||||
public void exit(View view) {
|
||||
finish();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical"
|
||||
android:padding="24dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/nfc_contact"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:textSize="26sp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/nfc_confirm"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="24dp"
|
||||
android:enabled="false"
|
||||
android:text="@android:string/ok"
|
||||
android:textSize="22sp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/nfc_cancel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@android:string/cancel"
|
||||
android:textSize="22sp" />
|
||||
</LinearLayout>
|
||||
</layout>
|
||||
@@ -122,6 +122,12 @@
|
||||
app:hintText="@string/hint_text_assistant"
|
||||
app:optionsText="@string/options_text_assistant" />
|
||||
|
||||
<Button
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:onClick="@{click::scanNfcContact}"
|
||||
android:text="扫描 NFC 联系人" />
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/dp_100">
|
||||
@@ -177,4 +183,4 @@
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</layout>
|
||||
</layout>
|
||||
@@ -0,0 +1,4 @@
|
||||
<resources>
|
||||
<bool name="nfc_legacy_dispatch">false</bool>
|
||||
<bool name="nfc_system_dispatch">true</bool>
|
||||
</resources>
|
||||
@@ -0,0 +1,4 @@
|
||||
<resources>
|
||||
<bool name="nfc_legacy_dispatch">true</bool>
|
||||
<bool name="nfc_system_dispatch">false</bool>
|
||||
</resources>
|
||||
Reference in new issue
Block a user