ConfigurationActivity.kt 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /******************************************************************************
  2. * *
  3. * Copyright (C) 2021 by nekohasekai <[email protected]> *
  4. * Copyright (C) 2021 by Max Lv <[email protected]> *
  5. * Copyright (C) 2021 by Mygod Studio <[email protected]> *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, either version 3 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  19. * *
  20. ******************************************************************************/
  21. package com.github.shadowsocks.plugin
  22. import android.app.Activity
  23. import android.content.Intent
  24. /**
  25. * Base class for configuration activity. A configuration activity is started when user wishes to configure the
  26. * selected plugin. To create a configuration activity, extend this class, implement abstract methods, invoke
  27. * `saveChanges(options)` and `discardChanges()` when appropriate, and add it to your manifest like this:
  28. *
  29. * <pre class="prettyprint">&lt;manifest&gt;
  30. * ...
  31. * &lt;application&gt;
  32. * ...
  33. * &lt;activity android:name=".ConfigureActivity"&gt;
  34. * &lt;intent-filter&gt;
  35. * &lt;action android:name="com.github.shadowsocks.plugin.ACTION_CONFIGURE"/&gt;
  36. * &lt;category android:name="android.intent.category.DEFAULT"/&gt;
  37. * &lt;data android:scheme="plugin"
  38. * android:host="com.github.shadowsocks"
  39. * android:path="/$PLUGIN_ID"/&gt;
  40. * &lt;/intent-filter&gt;
  41. * &lt;/activity&gt;
  42. * ...
  43. * &lt;/application&gt;
  44. *&lt;/manifest&gt;</pre>
  45. */
  46. abstract class ConfigurationActivity : OptionsCapableActivity() {
  47. /**
  48. * Equivalent to setResult(RESULT_CANCELED).
  49. */
  50. fun discardChanges() = setResult(Activity.RESULT_CANCELED)
  51. /**
  52. * Equivalent to setResult(RESULT_OK, args_with_correct_format).
  53. *
  54. * @param options PluginOptions to save.
  55. */
  56. fun saveChanges(options: PluginOptions) =
  57. setResult(Activity.RESULT_OK, Intent().putExtra(PluginContract.EXTRA_OPTIONS, options.toString()))
  58. /**
  59. * Finish this activity and request manual editor to pop up instead.
  60. */
  61. fun fallbackToManualEditor() {
  62. setResult(PluginContract.RESULT_FALLBACK)
  63. finish()
  64. }
  65. }