1 /* 2 * Copyright (C) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef APP_STATE_AWARE_H 17 #define APP_STATE_AWARE_H 18 19 #include "appmgr/app_mgr_interface.h" 20 #include "appmgr/app_state_data.h" 21 #include "iremote_object.h" 22 #include "appmgr/application_state_observer_stub.h" 23 24 namespace OHOS { 25 namespace NetManagerStandard { 26 27 constexpr uint32_t MAX_RETRY_COUNT = 5; 28 29 struct AppStateAwareCallback { 30 std::function<void(const uint32_t uid)> OnForegroundAppChanged; 31 }; 32 33 class AppStateObserver : public AppExecFwk::ApplicationStateObserverStub { 34 public: 35 void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override; 36 }; 37 38 class AppStateAwareManager { 39 public: 40 explicit AppStateAwareManager(); 41 ~AppStateAwareManager(); 42 static AppStateAwareManager &GetInstance(); 43 sptr<AppExecFwk::IAppMgr> GetAppMgr(); 44 void RegisterAppStateAwareCallback(const AppStateAwareCallback &appStateAwareCallback); 45 void RegisterAppStateObserver(); 46 bool SubscribeAppState(); 47 void UnSubscribeAppState(); 48 void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData); 49 bool IsForegroundApp(const uint32_t uid); 50 private: 51 std::atomic<int32_t> foregroundAppUid_; 52 std::mutex mutex_ {}; 53 sptr<AppStateObserver> appStateObserver_ = nullptr; 54 AppStateAwareCallback appStateAwareCallback_; 55 uint32_t retryCount_ = 0; 56 57 static std::mutex instanceMutex_; 58 }; 59 } // namespace NetManagerStandard 60 } // namespace OHOS 61 #endif 62