AbstractClickActivity.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2013-2015 by appPlant UG. All rights reserved.
  3. *
  4. * @APPPLANT_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPPLANT_LICENSE_HEADER_END@
  22. */
  23. package de.appplant.cordova.plugin.notification;
  24. import android.app.Activity;
  25. import android.content.Context;
  26. import android.content.Intent;
  27. import android.os.Bundle;
  28. import org.json.JSONException;
  29. import org.json.JSONObject;
  30. /**
  31. * Abstract content receiver activity for local notifications. Creates the
  32. * local notification and calls the event functions for further proceeding.
  33. */
  34. abstract public class AbstractClickActivity extends Activity {
  35. /**
  36. * Called when local notification was clicked to launch the main intent.
  37. *
  38. * @param state
  39. * Saved instance state
  40. */
  41. @Override
  42. public void onCreate (Bundle state) {
  43. super.onCreate(state);
  44. Intent intent = getIntent();
  45. Bundle bundle = intent.getExtras();
  46. Context context = getApplicationContext();
  47. try {
  48. String data = bundle.getString(Options.EXTRA);
  49. JSONObject options = new JSONObject(data);
  50. Builder builder =
  51. new Builder(context, options);
  52. Notification notification =
  53. buildNotification(builder);
  54. onClick(notification);
  55. } catch (JSONException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. /**
  60. * Fixes "Unable to resume activity" error.
  61. * Theme_NoDisplay: Activities finish themselves before being resumed.
  62. */
  63. @Override
  64. protected void onResume() {
  65. super.onResume();
  66. finish();
  67. }
  68. /**
  69. * Called when local notification was clicked by the user.
  70. *
  71. * @param notification
  72. * Wrapper around the local notification
  73. */
  74. abstract public void onClick (Notification notification);
  75. /**
  76. * Build notification specified by options.
  77. *
  78. * @param builder
  79. * Notification builder
  80. */
  81. abstract public Notification buildNotification (Builder builder);
  82. /**
  83. * Launch main intent from package.
  84. */
  85. public void launchApp() {
  86. Context context = getApplicationContext();
  87. String pkgName = context.getPackageName();
  88. Intent intent = context
  89. .getPackageManager()
  90. .getLaunchIntentForPackage(pkgName);
  91. intent.addFlags(
  92. Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  93. context.startActivity(intent);
  94. }
  95. }