|
| 1 | + |
| 2 | +package com.github.shadowsocks |
| 3 | + |
| 4 | +import android.app.{Activity, TaskStackBuilder} |
| 5 | +import android.content.Intent |
| 6 | +import android.content.pm.{PackageManager, ShortcutManager} |
| 7 | +import android.os.{Build, Bundle} |
| 8 | +import android.support.v4.app.ActivityCompat |
| 9 | +import android.support.v4.content.ContextCompat |
| 10 | +import android.support.v7.app.AppCompatActivity |
| 11 | +import android.support.v7.widget.Toolbar |
| 12 | +import android.text.TextUtils |
| 13 | +import android.widget.Toast |
| 14 | +import com.google.zxing.Result |
| 15 | +import com.github.shadowsocks.ShadowsocksApplication.app |
| 16 | +import com.github.shadowsocks.utils.Parser |
| 17 | +import me.dm7.barcodescanner.zxing.ZXingScannerView |
| 18 | + |
| 19 | +object ScannerActivity { |
| 20 | + private final val MY_PERMISSIONS_REQUEST_CAMERA = 1 |
| 21 | +} |
| 22 | + |
| 23 | +class ScannerActivity extends AppCompatActivity with ZXingScannerView.ResultHandler { |
| 24 | + import ScannerActivity._ |
| 25 | + |
| 26 | + private var scannerView: ZXingScannerView = _ |
| 27 | + |
| 28 | + override def onRequestPermissionsResult(requestCode: Int, permissions: Array[String], |
| 29 | + grantResults: Array[Int]) { |
| 30 | + if (requestCode == MY_PERMISSIONS_REQUEST_CAMERA) { |
| 31 | + // If request is cancelled, the result arrays are empty. |
| 32 | + if (grantResults.length > 0 |
| 33 | + && grantResults(0) == PackageManager.PERMISSION_GRANTED) { |
| 34 | + scannerView.setResultHandler(this) |
| 35 | + scannerView.startCamera() |
| 36 | + } else { |
| 37 | + Toast.makeText(this, R.string.add_profile_scanner_permission_required, Toast.LENGTH_SHORT).show() |
| 38 | + finish() |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + def navigateUp() { |
| 44 | + val intent = getParentActivityIntent |
| 45 | + if (shouldUpRecreateTask(intent) || isTaskRoot) |
| 46 | + { |
| 47 | + TaskStackBuilder.create(this).addNextIntentWithParentStack(intent).startActivities() |
| 48 | + } |
| 49 | + else finish() |
| 50 | + } |
| 51 | + |
| 52 | + override def onCreate(state: Bundle) { |
| 53 | + super.onCreate(state) |
| 54 | + setContentView(R.layout.layout_scanner) |
| 55 | + val toolbar = findViewById(R.id.toolbar).asInstanceOf[Toolbar] |
| 56 | + toolbar.setTitle(getTitle) |
| 57 | + toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_material) |
| 58 | + toolbar.setNavigationOnClickListener(_ => navigateUp()) |
| 59 | + scannerView = findViewById(R.id.scanner).asInstanceOf[ZXingScannerView] |
| 60 | + if (Build.VERSION.SDK_INT >= 25) getSystemService(classOf[ShortcutManager]).reportShortcutUsed("scan") |
| 61 | + } |
| 62 | + |
| 63 | + override def onResume() { |
| 64 | + super.onResume() |
| 65 | + val permissionCheck = ContextCompat.checkSelfPermission(this, |
| 66 | + android.Manifest.permission.CAMERA) |
| 67 | + if (permissionCheck == PackageManager.PERMISSION_GRANTED) { |
| 68 | + scannerView.setResultHandler(this) // Register ourselves as a handler for scan results. |
| 69 | + scannerView.setAutoFocus(true) |
| 70 | + scannerView.startCamera() // Start camera on resume |
| 71 | + } else { |
| 72 | + ActivityCompat.requestPermissions(this, |
| 73 | + Array(android.Manifest.permission.CAMERA), MY_PERMISSIONS_REQUEST_CAMERA) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + override def onPause() { |
| 78 | + super.onPause() |
| 79 | + scannerView.stopCamera() // Stop camera on pause |
| 80 | + } |
| 81 | + |
| 82 | + override def handleResult(rawResult: Result) = { |
| 83 | + val uri = rawResult.getText |
| 84 | + if (!TextUtils.isEmpty(uri)) |
| 85 | + { |
| 86 | + Parser.findAll(uri).foreach(app.profileManager.createProfile) |
| 87 | + Parser.findAll_ssr(uri).foreach(app.profileManager.createProfile) |
| 88 | + } |
| 89 | + navigateUp() |
| 90 | + } |
| 91 | +} |
| 92 | + |
0 commit comments