Home | History | Annotate | Download | only in service
      1 /*
      2  * Copyright (C) 2010 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.videoeditor.service;
     18 
     19 import java.util.ArrayList;
     20 import java.util.List;
     21 
     22 import android.content.ComponentName;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.os.Bundle;
     26 import android.util.Log;
     27 
     28 /**
     29  * A pool of Intents
     30  */
     31 class IntentPool {
     32     // Logging
     33     private static final String TAG = "IntentPool";
     34 
     35     // Instance variables
     36     private final List<Intent> mIntentPool;
     37 
     38     /**
     39      * Constructor
     40      *
     41      * @param initialSize The initial size of the pool
     42      */
     43     public IntentPool(int initialSize) {
     44         mIntentPool = new ArrayList<Intent>(initialSize);
     45     }
     46 
     47     /**
     48      * @return The Intent is retrieved from the pool or if the pool is empty
     49      *      a new Intent is allocated
     50      */
     51     public synchronized Intent get(Context context, Class<?> cls) {
     52         final Intent intent = get();
     53         intent.setComponent(new ComponentName(context, cls));
     54         return intent;
     55     }
     56 
     57     /**
     58      * @return The Intent is retrieved from the pool or if the pool is empty
     59      *      a new Intent is allocated
     60      */
     61     public synchronized Intent get() {
     62         if (mIntentPool.size() > 0) {
     63             final Intent intent = mIntentPool.remove(0);
     64             // Clear the content of the Intent
     65             final Bundle extras = intent.getExtras();
     66             for (String keys : extras.keySet()) {
     67                 intent.removeExtra(keys);
     68             }
     69             intent.setComponent(null);
     70             return intent;
     71         } else {
     72             if (Log.isLoggable(TAG, Log.DEBUG)) {
     73                 Log.d(TAG, "Pool enlarged");
     74             }
     75             return new Intent();
     76         }
     77     }
     78 
     79     /**
     80      * @param intent Return an Intent to the pool
     81      */
     82     public synchronized void put(Intent intent) {
     83         mIntentPool.add(intent);
     84     }
     85 }
     86