nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 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 | |
| 17 | package com.android.nfc; |
| 18 | |
| 19 | import android.app.ActivityManager; |
| 20 | import android.app.ActivityManager.RunningAppProcessInfo; |
| 21 | import android.app.Application; |
| 22 | import android.os.Process; |
| 23 | import android.os.UserHandle; |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 24 | import android.view.ThreadedRenderer; |
nxpandroid | 2441c13 | 2017-09-14 11:56:39 +0530 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 26 | |
| 27 | import java.util.Iterator; |
| 28 | import java.util.List; |
| 29 | |
| 30 | public class NfcApplication extends Application { |
| 31 | |
| 32 | static final String TAG = "NfcApplication"; |
| 33 | static final String NFC_PROCESS = "com.android.nfc"; |
| 34 | |
| 35 | NfcService mNfcService; |
| 36 | |
| 37 | public NfcApplication() { |
| 38 | |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public void onCreate() { |
| 43 | super.onCreate(); |
nxpandroid | 2441c13 | 2017-09-14 11:56:39 +0530 | [diff] [blame] | 44 | PackageManager pm = getApplicationContext().getPackageManager(); |
| 45 | if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_ANY)) { |
nxpandroid | 2441c13 | 2017-09-14 11:56:39 +0530 | [diff] [blame] | 46 | return; |
| 47 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 48 | |
| 49 | boolean isMainProcess = false; |
| 50 | // We start a service in a separate process to do |
| 51 | // handover transfer. We don't want to instantiate an NfcService |
| 52 | // object in those cases, hence check the name of the process |
| 53 | // to determine whether we're the main NFC service, or the |
| 54 | // handover process |
| 55 | ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE); |
| 56 | List processes = am.getRunningAppProcesses(); |
| 57 | Iterator i = processes.iterator(); |
| 58 | while (i.hasNext()) { |
| 59 | RunningAppProcessInfo appInfo = (RunningAppProcessInfo)(i.next()); |
| 60 | if (appInfo.pid == Process.myPid()) { |
| 61 | isMainProcess = (NFC_PROCESS.equals(appInfo.processName)); |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | if (UserHandle.myUserId() == 0 && isMainProcess) { |
| 66 | mNfcService = new NfcService(this); |
nxpandroid | 281eb92 | 2016-08-25 20:27:46 +0530 | [diff] [blame] | 67 | ThreadedRenderer.enableForegroundTrimming(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |