Home | History | Annotate | Download | only in logcat
      1 /*
      2  * Copyright (C) 2011 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 package com.android.ddmuilib.logcat;
     17 
     18 import com.android.ddmlib.AndroidDebugBridge;
     19 import com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener;
     20 import com.android.ddmlib.IDevice;
     21 
     22 import org.eclipse.jface.preference.IPreferenceStore;
     23 
     24 import java.util.HashMap;
     25 import java.util.Map;
     26 
     27 /**
     28  * A factory for {@link LogCatReceiver} objects. Its primary objective is to cache
     29  * constructed {@link LogCatReceiver}'s per device and hand them back when requested.
     30  */
     31 public class LogCatReceiverFactory {
     32     /** Singleton instance. */
     33     public static final LogCatReceiverFactory INSTANCE = new LogCatReceiverFactory();
     34 
     35     private Map<String, LogCatReceiver> mReceiverCache = new HashMap<String, LogCatReceiver>();
     36 
     37     /** Private constructor: cannot instantiate. */
     38     private LogCatReceiverFactory() {
     39         AndroidDebugBridge.addDeviceChangeListener(new IDeviceChangeListener() {
     40             public void deviceDisconnected(IDevice device) {
     41                 removeReceiverFor(device);
     42             }
     43 
     44             public void deviceConnected(IDevice device) {
     45             }
     46 
     47             public void deviceChanged(IDevice device, int changeMask) {
     48             }
     49         });
     50     }
     51 
     52     private synchronized void removeReceiverFor(IDevice device) {
     53         mReceiverCache.remove(device.getSerialNumber());
     54     }
     55 
     56     public synchronized LogCatReceiver newReceiver(IDevice device, IPreferenceStore prefs) {
     57         LogCatReceiver r = mReceiverCache.get(device.getSerialNumber());
     58         if (r != null) {
     59             return r;
     60         }
     61 
     62         r = new LogCatReceiver(device, prefs);
     63         mReceiverCache.put(device.getSerialNumber(), r);
     64         return r;
     65     }
     66 }
     67