Add share profile by NFC.
This commit is contained in:
@@ -39,10 +39,11 @@
|
||||
|
||||
package com.github.shadowsocks
|
||||
|
||||
import android.content.DialogInterface
|
||||
import android.content.{Intent, DialogInterface}
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.os.Bundle
|
||||
import android.nfc.{NdefMessage, NfcAdapter}
|
||||
import android.os.{Parcelable, Bundle}
|
||||
import android.support.v7.app.{AppCompatActivity, AlertDialog}
|
||||
import android.view.WindowManager
|
||||
import com.github.shadowsocks.utils.Parser
|
||||
@@ -50,9 +51,24 @@ import com.github.shadowsocks.utils.Parser
|
||||
class ParserActivity extends AppCompatActivity {
|
||||
override def onCreate(savedInstanceState: Bundle) {
|
||||
super.onCreate(savedInstanceState)
|
||||
val data = getIntent.getData.toString
|
||||
val profiles = Parser.findAll(data)
|
||||
if (profiles.isEmpty) { // ignore
|
||||
}
|
||||
|
||||
|
||||
override def onResume() {
|
||||
super.onResume()
|
||||
var sharedStr = ""
|
||||
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent.getAction)) {
|
||||
val rawMsgs = getIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
|
||||
if (rawMsgs != null) {
|
||||
val msg = rawMsgs(0).asInstanceOf[NdefMessage]
|
||||
val records = msg.getRecords
|
||||
sharedStr = new String(records(0).getPayload)
|
||||
}
|
||||
} else if (Intent.ACTION_VIEW.equals(getIntent.getAction)) {
|
||||
sharedStr = getIntent.getData.toString
|
||||
}
|
||||
val profiles = Parser.findAll(sharedStr)
|
||||
if (profiles.isEmpty) {
|
||||
finish()
|
||||
return
|
||||
}
|
||||
@@ -63,9 +79,9 @@ class ParserActivity extends AppCompatActivity {
|
||||
.setPositiveButton(android.R.string.yes, ((_, _) =>
|
||||
profiles.foreach(ShadowsocksApplication.profileManager.createProfile)): DialogInterface.OnClickListener)
|
||||
.setNegativeButton(android.R.string.no, null)
|
||||
.setMessage(data)
|
||||
.setMessage(sharedStr)
|
||||
.create()
|
||||
dialog.setOnDismissListener((dialog: DialogInterface) => finish()) // builder method was added in API 17
|
||||
dialog.setOnDismissListener((dialog: DialogInterface) => finish()) // builder method was added in API 17
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.github.shadowsocks
|
||||
|
||||
import java.nio.charset.Charset
|
||||
|
||||
import android.content._
|
||||
import android.nfc.{NdefRecord, NdefMessage, NfcEvent, NfcAdapter}
|
||||
import android.nfc.NfcAdapter.CreateNdefMessageCallback
|
||||
import android.os.{Bundle, Handler}
|
||||
import android.support.v7.app.{AlertDialog, AppCompatActivity}
|
||||
import android.support.v7.widget.RecyclerView.ViewHolder
|
||||
@@ -24,16 +28,20 @@ import scala.collection.mutable.ArrayBuffer
|
||||
/**
|
||||
* @author Mygod
|
||||
*/
|
||||
class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListener with ServiceBoundContext {
|
||||
class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListener with ServiceBoundContext with CreateNdefMessageCallback{
|
||||
private class ProfileViewHolder(val view: View) extends RecyclerView.ViewHolder(view) with View.OnClickListener {
|
||||
var item: Profile = _
|
||||
private val text = itemView.findViewById(android.R.id.text1).asInstanceOf[CheckedTextView]
|
||||
itemView.setOnClickListener(this)
|
||||
|
||||
{
|
||||
val qrcode = itemView.findViewById(R.id.qrcode)
|
||||
qrcode.setOnClickListener(_ => {
|
||||
val shareBtn = itemView.findViewById(R.id.share)
|
||||
shareBtn.setOnClickListener(_ => {
|
||||
val url = Parser.generate(item)
|
||||
if (nfcAdapter != null && nfcAdapter.isEnabled) {
|
||||
nfcAdapter.setNdefPushMessageCallback(ProfileManagerActivity.this,ProfileManagerActivity.this)
|
||||
nfcShareItem = url
|
||||
}
|
||||
val image = new ImageView(ProfileManagerActivity.this)
|
||||
image.setLayoutParams(new LinearLayout.LayoutParams(-1, -1))
|
||||
val qrcode = QRCode.from(url)
|
||||
@@ -41,17 +49,24 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
|
||||
.asInstanceOf[QRCode].bitmap()
|
||||
image.setImageBitmap(qrcode)
|
||||
|
||||
new AlertDialog.Builder(ProfileManagerActivity.this)
|
||||
val dialog = new AlertDialog.Builder(ProfileManagerActivity.this)
|
||||
.setCancelable(true)
|
||||
.setPositiveButton(R.string.close, null)
|
||||
.setNegativeButton(R.string.copy_url, ((_, _) =>
|
||||
clipboard.setPrimaryClip(ClipData.newPlainText(null, url))): DialogInterface.OnClickListener)
|
||||
.setView(image)
|
||||
.setMessage(R.string.share_message)
|
||||
.setTitle(R.string.share)
|
||||
.create()
|
||||
.show()
|
||||
dialog.show()
|
||||
dialog.setOnDismissListener((dialog : DialogInterface) => {
|
||||
if (nfcAdapter != null && nfcAdapter.isEnabled) {
|
||||
nfcAdapter.setNdefPushMessageCallback(null,ProfileManagerActivity.this)
|
||||
}
|
||||
})
|
||||
})
|
||||
qrcode.setOnLongClickListener((v: View) => {
|
||||
Utils.positionToast(Toast.makeText(ProfileManagerActivity.this, R.string.qrcode, Toast.LENGTH_SHORT), qrcode,
|
||||
shareBtn.setOnLongClickListener((v: View) => {
|
||||
Utils.positionToast(Toast.makeText(ProfileManagerActivity.this, R.string.share, Toast.LENGTH_SHORT), shareBtn,
|
||||
getWindow, 0, Utils.dpToPx(ProfileManagerActivity.this, 8)).show
|
||||
true
|
||||
})
|
||||
@@ -148,6 +163,8 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
|
||||
private var undoManager: UndoSnackbarManager[Profile] = _
|
||||
|
||||
private lazy val clipboard = getSystemService(Context.CLIPBOARD_SERVICE).asInstanceOf[ClipboardManager]
|
||||
private var nfcAdapter : NfcAdapter = _
|
||||
private var nfcShareItem : String = _
|
||||
|
||||
override def onCreate(savedInstanceState: Bundle) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -163,6 +180,11 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
|
||||
toolbar.inflateMenu(R.menu.profile_manager_menu)
|
||||
toolbar.setOnMenuItemClickListener(this)
|
||||
|
||||
nfcAdapter = NfcAdapter.getDefaultAdapter(this)
|
||||
if (nfcAdapter != null && nfcAdapter.isEnabled) {
|
||||
nfcAdapter.setNdefPushMessageCallback(null,ProfileManagerActivity.this)
|
||||
}
|
||||
|
||||
ShadowsocksApplication.profileManager.setProfileAddedListener(profilesAdapter.add)
|
||||
val profilesList = findViewById(R.id.profilesList).asInstanceOf[RecyclerView]
|
||||
val layoutManager = new LinearLayoutManager(this)
|
||||
@@ -221,6 +243,12 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
|
||||
Parser.findAll(scanResult.getContents).foreach(ShadowsocksApplication.profileManager.createProfile)
|
||||
}
|
||||
|
||||
def createNdefMessage(nfcEvent: NfcEvent): NdefMessage = {
|
||||
val ndefRecord = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI,nfcShareItem.getBytes(Charset.forName("US-ASCII")),Array[Byte](),nfcShareItem.getBytes(Charset.forName("US-ASCII")))
|
||||
val ndefMessage = new NdefMessage(Array[NdefRecord](ndefRecord))
|
||||
ndefMessage
|
||||
}
|
||||
|
||||
def onMenuItemClick(item: MenuItem): Boolean = item.getItemId match {
|
||||
case R.id.scan_qr_code =>
|
||||
val integrator = new IntentIntegrator(this)
|
||||
|
||||
Reference in New Issue
Block a user