Home | History | Annotate | Download | only in nfc
      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;
     24 import android.view.ThreadedRenderer;
     25 
     26 import java.util.Iterator;
     27 import java.util.List;
     28 
     29 public class NfcApplication extends Application {
     30 
     31     static final String TAG = "NfcApplication";
     32     static final String NFC_PROCESS = "com.android.nfc";
     33 
     34     NfcService mNfcService;
     35 
     36     public NfcApplication() {
     37 
     38     }
     39 
     40     @Override
     41     public void onCreate() {
     42         super.onCreate();
     43 
     44         boolean isMainProcess = false;
     45         // We start a service in a separate process to do
     46         // handover transfer. We don't want to instantiate an NfcService
     47         // object in those cases, hence check the name of the process
     48         // to determine whether we're the main NFC service, or the
     49         // handover process
     50         ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
     51         List processes = am.getRunningAppProcesses();
     52         Iterator i = processes.iterator();
     53         while (i.hasNext()) {
     54             RunningAppProcessInfo appInfo = (RunningAppProcessInfo)(i.next());
     55             if (appInfo.pid == Process.myPid()) {
     56                 isMainProcess =  (NFC_PROCESS.equals(appInfo.processName));
     57                 break;
     58             }
     59         }
     60         if (UserHandle.myUserId() == 0 && isMainProcess) {
     61             mNfcService = new NfcService(this);
     62             ThreadedRenderer.enableForegroundTrimming();
     63         }
     64     }
     65 }
     66