Activity.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Activity.h
  3. *
  4. * Created on: Aug 21, 2017
  5. * Author: guoxs
  6. */
  7. #ifndef _APP_ACTIVITY_H_
  8. #define _APP_ACTIVITY_H_
  9. #include <string>
  10. #include <map>
  11. #include "BaseApp.h"
  12. class Intent;
  13. class Activity : public BaseApp {
  14. friend class ActivityStack;
  15. public:
  16. Activity();
  17. virtual ~Activity();
  18. int style() const { return mStyle; }
  19. bool needSwitchEffect() const { return mNeedSwitchEffect; }
  20. protected:
  21. /**
  22. * @brief 界面传递数据时回调
  23. */
  24. virtual void onIntent(const Intent *intentPtr) { }
  25. /**
  26. * @brief 界面显示时回调
  27. */
  28. virtual void onResume() { }
  29. /**
  30. * @brief 界面隐藏时回调
  31. */
  32. virtual void onPause() { }
  33. /**
  34. * @brief 执行返回时回调
  35. * @return true 允许返回,false 禁止返回
  36. */
  37. virtual bool onBack() { return true; }
  38. typedef enum {
  39. E_STYLE_PUSH = 0x01,
  40. E_STYLE_ZOOM,
  41. E_STYLE_ALPHA
  42. } EStyle;
  43. protected:
  44. int mStyle;
  45. int mPushIndex;
  46. bool mNeedSwitchEffect;
  47. };
  48. class Intent {
  49. public:
  50. typedef enum {
  51. E_INTENT_ACTION_MAIN,
  52. E_INTENT_ACTION_VIEW,
  53. E_INTENT_ACTION_EDIT
  54. } EIntentAction;
  55. Intent(int action = E_INTENT_ACTION_MAIN, std::string uri = "") :
  56. mAction(action), mUri(uri) { }
  57. virtual ~Intent() { }
  58. public:
  59. int getAction() const { return mAction; }
  60. const std::string& getUri() const { return mUri; }
  61. /**
  62. * @brief 设置键值数据
  63. * @note 统一设置为string类型,如int类型需转为string类型,再存储,getExtra的时候再转一下
  64. */
  65. void putExtra(const std::string &key, const std::string &value) {
  66. mExtras[key] = value;
  67. }
  68. /**
  69. * @brief 获取key对应的数据
  70. */
  71. const std::string& getExtra(const std::string &key) const {
  72. ExtraMap::const_iterator found = mExtras.find(key);
  73. static std::string empty("");
  74. if (found == mExtras.end()) {
  75. return empty;
  76. } else {
  77. return found->second;
  78. }
  79. }
  80. protected:
  81. typedef std::map<std::string, std::string> ExtraMap;
  82. int mAction;
  83. std::string mUri;
  84. ExtraMap mExtras;
  85. };
  86. #include <vector>
  87. class ActivityFactory {
  88. public:
  89. static ActivityFactory *getInstance();
  90. bool registerActivity(const char *name, Activity* (*create)());
  91. Activity* create(const char *name);
  92. void dumpActivityInfos();
  93. void queryNames(std::vector<std::string> &names);
  94. private:
  95. ActivityFactory() { }
  96. private:
  97. typedef struct {
  98. std::string activityName;
  99. Activity* (*create)();
  100. } SActivityInfo;
  101. std::vector<SActivityInfo> mActivityInfos;
  102. };
  103. #define ACTIVITYFACTORY ActivityFactory::getInstance()
  104. #define REGISTER_ACTIVITY(_class) \
  105. static struct _ActivityFactory_##_class { \
  106. static Activity* create() { \
  107. return new _class(); \
  108. } \
  109. _ActivityFactory_##_class() { \
  110. ACTIVITYFACTORY->registerActivity(#_class, create); \
  111. } \
  112. } _autoRegister_Activity_##_class
  113. #endif /* _APP_ACTIVITY_H_ */