/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.helloactivity;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.felix.framework.Felix;
import org.apache.felix.framework.cache.BundleCache;
import org.apache.felix.framework.util.StringMap;
import org.apache.felix.main.AutoProcessor;
import org.osgi.framework.BundleContext;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


/**
 * A minimal "Hello, World!" application.
 */
public class HelloActivity extends Activity {
	
    private Felix m_felix = null;

	
    public HelloActivity() {
    }

    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the layout for this activity.  You can find it
        // in res/layout/hello_activity.xml
        setContentView(R.layout.hello_activity);
        
        launchFelix();
        
    }
    
    void launchFelix() {
    	
    	Log.d("Felix", "About to start Felix...");
    	
    	// Create a configuration property map
    	
    	String cacheDir = null;
    	
    	try {
			cacheDir = File.createTempFile("skifta", ".tmp").getParent();
		} catch (IOException e) {
			Log.d("Felix", "unable to create temp file", e);
			return;
		}
        
        Map configMap = new StringMap(false);
                    
        configMap.put("org.osgi.framework.storage", cacheDir);
        configMap.put("felix.embedded.execution", "true");
        configMap.put("org.osgi.service.http.port", "9990");
        configMap.put("org.osgi.framework.startlevel.beginning", "5");

        try
        {
            // Now create an instance of the framework with
            // our configuration properties.
            m_felix = new Felix(configMap);
            // Now start Felix instance.
            m_felix.start();
           
            Log.d("Felix", "Felix is started");
            
            for(org.osgi.framework.Bundle b : m_felix.getBundleContext().getBundles()) {
            	
            	Log.d("Felix", "Bundle: " + b.getSymbolicName());
            	
            }
            
        } catch (Throwable ex) {
            Log.d("Felix","Could not create framework: " + ex.getMessage(), ex);
        }

    	
    }
    
   
}

