From 8a86176ded8a6579a6484e46d753536cde004876 Mon Sep 17 00:00:00 2001 From: Lucas Liu Date: Fri, 18 Mar 2016 17:44:58 +0800 Subject: [PATCH] Handle share intent in ProfileManagerActivity --- src/main/AndroidManifest.xml | 36 +++--- .../github/shadowsocks/ParserActivity.scala | 104 ------------------ .../shadowsocks/ProfileManagerActivity.scala | 45 ++++++++ 3 files changed, 59 insertions(+), 126 deletions(-) delete mode 100644 src/main/scala/com/github/shadowsocks/ParserActivity.scala diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 5d445299..73e0bfdb 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -47,11 +47,21 @@ + + + + + + + @@ -65,24 +75,6 @@ - - - - - - - - - - - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * - * ___====-_ _-====___ - * _--^^^#####// \\#####^^^--_ - * _-^##########// ( ) \\##########^-_ - * -############// |\^^/| \\############- - * _/############// (@::@) \\############\_ - * /#############(( \\// ))#############\ - * -###############\\ (oo) //###############- - * -#################\\ / VV \ //#################- - * -###################\\/ \//###################- - * _#/|##########/\######( /\ )######/\##########|\#_ - * |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \| - * ` |/ V V ` V \#\| | | |/#/ V ' V V \| ' - * ` ` ` ` / | | | | \ ' ' ' ' - * ( | | | | ) - * __\ | | | | /__ - * (vvv(VVV)(VVV)vvv) - * - * HERE BE DRAGONS - * - */ - -package com.github.shadowsocks - -import android.content.{DialogInterface, Intent} -import android.graphics.Color -import android.graphics.drawable.ColorDrawable -import android.nfc.{NdefMessage, NfcAdapter} -import android.os.Bundle -import android.support.v7.app.{AlertDialog, AppCompatActivity} -import android.text.TextUtils -import android.view.WindowManager -import com.github.shadowsocks.utils.Parser - -class ParserActivity extends AppCompatActivity { - - override def onCreate(savedInstanceState: Bundle) { - super.onCreate(savedInstanceState) - handledSharedIntent() - } - - override def onNewIntent(intent: Intent) { - super.onNewIntent(intent) - setIntent(intent) - handledSharedIntent() - } - - def handledSharedIntent() { - val intent = getIntent() - val sharedStr = intent.getAction match { - case Intent.ACTION_VIEW => intent.getData.toString - case NfcAdapter.ACTION_NDEF_DISCOVERED => - val rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES) - if (rawMsgs != null && rawMsgs.nonEmpty) - new String(rawMsgs(0).asInstanceOf[NdefMessage].getRecords()(0).getPayload) - else null - case _ => null - } - if (TextUtils.isEmpty(sharedStr)) return - val profiles = Parser.findAll(sharedStr).toList - if (profiles.isEmpty) { - finish() - return - } - showAsPopup() - val dialog = new AlertDialog.Builder(this) - .setTitle(R.string.add_profile_dialog) - .setCancelable(false) - .setPositiveButton(android.R.string.yes, ((_, _) => - profiles.foreach(ShadowsocksApplication.profileManager.createProfile)): DialogInterface.OnClickListener) - .setNegativeButton(android.R.string.no, null) - .setMessage(profiles.mkString("\n")) - .create() - dialog.setOnDismissListener((_: DialogInterface) => finish()) // builder method was added in API 17 - dialog.show() - } - - def showAsPopup() { - val window = getWindow - window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND) - val params = window.getAttributes - params.alpha = 1.0f - params.dimAmount = 0.5f - window.setAttributes(params.asInstanceOf[android.view.WindowManager.LayoutParams]) - window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)) - } -} diff --git a/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala b/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala index 36d8b84d..041b006d 100644 --- a/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala +++ b/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala @@ -7,6 +7,7 @@ import android.nfc.NfcAdapter.CreateNdefMessageCallback import android.nfc.{NdefMessage, NdefRecord, NfcAdapter, NfcEvent} import android.os.{Build, Bundle, Handler} import android.provider.Settings +import android.support.v4.app.{NavUtils, TaskStackBuilder} import android.support.v7.app.{AlertDialog, AppCompatActivity} import android.support.v7.widget.RecyclerView.ViewHolder import android.support.v7.widget.Toolbar.OnMenuItemClickListener @@ -113,6 +114,9 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe def onClick(v: View) = { ShadowsocksApplication.switchProfile(item.id) finish + if (isLauncherFromShareIntent){ + TaskStackBuilder.create(ProfileManagerActivity.this).addNextIntentWithParentStack(NavUtils.getParentActivityIntent(ProfileManagerActivity.this)).startActivities() + } } } @@ -181,6 +185,8 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe private var nfcEnable = false private var nfcBeamEnable = false + private var isLauncherFromShareIntent = false + override def onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) setContentView(R.layout.layout_profiles) @@ -234,6 +240,17 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe override def onResume() { super.onResume() + isLauncherFromShareIntent = false + handledShareIntent() + updateNFCState() + } + + override def onNewIntent(intent: Intent): Unit ={ + super.onNewIntent(intent) + setIntent(intent) + } + + def updateNFCState(): Unit = { nfcAdapter = NfcAdapter.getDefaultAdapter(this) if (nfcAdapter != null){ nfcAvailable = true @@ -256,6 +273,34 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe } } + def handledShareIntent() { + val intent = getIntent() + val sharedStr = intent.getAction match { + case Intent.ACTION_VIEW => intent.getData.toString + case NfcAdapter.ACTION_NDEF_DISCOVERED => + val rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES) + if (rawMsgs != null && rawMsgs.nonEmpty) + new String(rawMsgs(0).asInstanceOf[NdefMessage].getRecords()(0).getPayload) + else null + case _ => null + } + if (TextUtils.isEmpty(sharedStr)) return + val profiles = Parser.findAll(sharedStr).toList + if (profiles.isEmpty) { + finish() + return + } + val dialog = new AlertDialog.Builder(this) + .setTitle(R.string.add_profile_dialog) + .setPositiveButton(android.R.string.yes, ((_, _) => + profiles.foreach(ShadowsocksApplication.profileManager.createProfile)): DialogInterface.OnClickListener) + .setNegativeButton(android.R.string.no, ((_, _) => finish()): DialogInterface.OnClickListener) + .setMessage(profiles.mkString("\n")) + .create() + dialog.show() + isLauncherFromShareIntent = true + } + override def onStart() { super.onStart() registerCallback