ServerManager.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright © 2018 Zhenjie Yan.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.rdgk.cemetery.component;
  17. import android.content.BroadcastReceiver;
  18. import android.content.Context;
  19. import android.content.Intent;
  20. import android.content.IntentFilter;
  21. import com.rdgk.cemetery.activity.MainActivity;
  22. /**
  23. * Created by Zhenjie Yan on 2018/6/9.
  24. */
  25. public class ServerManager extends BroadcastReceiver {
  26. private static final String ACTION = "com.rdgk.cemetery.Receiver";
  27. private static final String CMD_KEY = "CMD_KEY";
  28. private static final String MESSAGE_KEY = "MESSAGE_KEY";
  29. private static final int CMD_VALUE_START = 1;
  30. private static final int CMD_VALUE_ERROR = 2;
  31. private static final int CMD_VALUE_STOP = 4;
  32. /**
  33. * Notify serverStart.
  34. *
  35. * @param context context.
  36. */
  37. public static void onServerStart(Context context, String hostAddress) {
  38. sendBroadcast(context, CMD_VALUE_START, hostAddress);
  39. }
  40. /**
  41. * Notify serverStop.
  42. *
  43. * @param context context.
  44. */
  45. public static void onServerError(Context context, String error) {
  46. sendBroadcast(context, CMD_VALUE_ERROR, error);
  47. }
  48. /**
  49. * Notify serverStop.
  50. *
  51. * @param context context.
  52. */
  53. public static void onServerStop(Context context) {
  54. sendBroadcast(context, CMD_VALUE_STOP);
  55. }
  56. private static void sendBroadcast(Context context, int cmd) {
  57. sendBroadcast(context, cmd, null);
  58. }
  59. private static void sendBroadcast(Context context, int cmd, String message) {
  60. Intent broadcast = new Intent(ACTION);
  61. broadcast.putExtra(CMD_KEY, cmd);
  62. broadcast.putExtra(MESSAGE_KEY, message);
  63. context.sendBroadcast(broadcast);
  64. }
  65. private MainActivity mActivity;
  66. private Intent mService;
  67. public ServerManager(MainActivity activity) {
  68. this.mActivity = activity;
  69. mService = new Intent(activity, CoreService.class);
  70. }
  71. /**
  72. * Register broadcast.
  73. */
  74. public void register() {
  75. IntentFilter filter = new IntentFilter(ACTION);
  76. mActivity.registerReceiver(this, filter);
  77. }
  78. /**
  79. * UnRegister broadcast.
  80. */
  81. public void unRegister() {
  82. mActivity.unregisterReceiver(this);
  83. }
  84. public void startServer() {
  85. mActivity.startService(mService);
  86. }
  87. public void stopServer() {
  88. mActivity.stopService(mService);
  89. }
  90. @Override
  91. public void onReceive(Context context, Intent intent) {
  92. String action = intent.getAction();
  93. if (ACTION.equals(action)) {
  94. int cmd = intent.getIntExtra(CMD_KEY, 0);
  95. switch (cmd) {
  96. case CMD_VALUE_START: {
  97. String ip = intent.getStringExtra(MESSAGE_KEY);
  98. mActivity.onServerStart(ip);
  99. break;
  100. }
  101. case CMD_VALUE_ERROR: {
  102. String error = intent.getStringExtra(MESSAGE_KEY);
  103. mActivity.onServerError(error);
  104. break;
  105. }
  106. case CMD_VALUE_STOP: {
  107. mActivity.onServerStop();
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. }