<commit_before>/*
 * DTNClient.java
 * 
 * Copyright (C) 2011 IBR, TU Braunschweig
 *
 * Written-by: <NAME> <<EMAIL>>
 *
 * 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 de.tubs.ibr.dtn.api;

import java.util.List;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
import android.os.RemoteException;
import android.util.Log;
import de.tubs.ibr.dtn.DTNService;

public final class DTNClient {
	
	private static final String TAG = "DTNClient";

    // DTN service provided by IBR-DTN
    private DTNService mService = null;
    
    // session object
	private Session mSession = null;
	
	// data handler which processes incoming bundles
	private DataHandler mHandler = null;
	
	private Context mContext = null;
	
	private Registration mRegistration = null;
	
    private Boolean mBlocking = true;
    
    private String mPackageName = null;
    
    private SessionConnection mSessionHandler = null;
    
    private Boolean mShutdown = false;
    
    public DTNClient(SessionConnection handler) {
    	this.mSessionHandler = handler;
    }
    
    public DTNClient() {
    	// add dummy handler
    	this.mSessionHandler = new SessionConnection() {
			public void onSessionConnected(Session session) { }

			public void onSessionDisconnected() { }
    	};
    }
	
	/**
	 * If blocking is enabled, the getSession() method will block until a valid session
	 * exists. If set to false, the method would return null if there is no active session.
	 * @param val
	 */
	public synchronized void setBlocking(Boolean val) {
		mBlocking = val;
	}

	public synchronized void setDataHandler(DataHandler handler) {
		this.mHandler = handler;
	}
	
	public synchronized Session getSession() throws SessionDestroyedException, InterruptedException {
		if (mBlocking)
		{
			while (mSession == null)
			{
				if (mShutdown) throw new SessionDestroyedException();
				wait();
			}
			
			return mSession;
		}
		else
		{
			if ((mSession == null) || mShutdown) throw new SessionDestroyedException();
			return mSession;
		}
	}
	   
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName name, IBinder service) {
			DTNClient.this.mService = DTNService.Stub.asInterface(service);
			initializeSession(mRegistration);
		}

		public void onServiceDisconnected(ComponentName name) {
			mSessionHandler.onSessionDisconnected();
			DTNClient.this.mService = null;
		}
	};
	
	private BroadcastReceiver mStateReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(de.tubs.ibr.dtn.Intent.REGISTRATION))
			{
				Log.i(TAG, "registration successful");
				initializeSession(mRegistration);
			}
		}
	};
	
	private void initializeSession(Registration reg) {
		try {
			Session s = new Session(mService, mPackageName);
			
			s.initialize();
			
			synchronized(DTNClient.this) {
				mSession = s;
				notifyAll();
			}
			
			mSessionHandler.onSessionConnected(s);
		} catch (Exception e) {
	    	Intent registrationIntent = new Intent(de.tubs.ibr.dtn.Intent.REGISTER);
	    	registrationIntent.putExtra("app", PendingIntent.getBroadcast(mContext, 0, new Intent(), 0)); // boilerplate
	    	registrationIntent.putExtra("registration", (Parcelable)reg);
	    	mContext.startService(registrationIntent);
		}
	}
	
	private de.tubs.ibr.dtn.api.DTNSessionCallback mCallback = new de.tubs.ibr.dtn.api.DTNSessionCallback.Stub() {
		public void startBundle(Bundle bundle) throws RemoteException {
			if (mHandler == null) return;
			mHandler.startBundle(bundle);
		}

		public void endBundle() throws RemoteException {
			if (mHandler == null) return;
			mHandler.endBundle();
		}
<commit_msg>Only initialize session if the service is connected<commit_after>/*
 * DTNClient.java
 * 
 * Copyright (C) 2011 IBR, TU Braunschweig
 *
 * Written-by: <NAME> <<EMAIL>>
 *
 * 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 de.tubs.ibr.dtn.api;

import java.util.List;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
import android.os.RemoteException;
import android.util.Log;
import de.tubs.ibr.dtn.DTNService;

public final class DTNClient {
	
	private static final String TAG = "DTNClient";

    // DTN service provided by IBR-DTN
    private DTNService mService = null;
    
    // session object
	private Session mSession = null;
	
	// data handler which processes incoming bundles
	private DataHandler mHandler = null;
	
	private Context mContext = null;
	
	private Registration mRegistration = null;
	
    private Boolean mBlocking = true;
    
    private String mPackageName = null;
    
    private SessionConnection mSessionHandler = null;
    
    private Boolean mShutdown = false;
    
    public DTNClient(SessionConnection handler) {
    	this.mSessionHandler = handler;
    }
    
    public DTNClient() {
    	// add dummy handler
    	this.mSessionHandler = new SessionConnection() {
			public void onSessionConnected(Session session) { }

			public void onSessionDisconnected() { }
    	};
    }
	
	/**
	 * If blocking is enabled, the getSession() method will block until a valid session
	 * exists. If set to false, the method would return null if there is no active session.
	 * @param val
	 */
	public synchronized void setBlocking(Boolean val) {
		mBlocking = val;
	}

	public synchronized void setDataHandler(DataHandler handler) {
		this.mHandler = handler;
	}
	
	public synchronized Session getSession() throws SessionDestroyedException, InterruptedException {
		if (mBlocking)
		{
			while (mSession == null)
			{
				if (mShutdown) throw new SessionDestroyedException();
				wait();
			}
			
			return mSession;
		}
		else
		{
			if ((mSession == null) || mShutdown) throw new SessionDestroyedException();
			return mSession;
		}
	}
	   
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName name, IBinder service) {
			DTNClient.this.mService = DTNService.Stub.asInterface(service);
			initializeSession(mRegistration);
		}

		public void onServiceDisconnected(ComponentName name) {
			mSessionHandler.onSessionDisconnected();
			DTNClient.this.mService = null;
		}
	};
	
	private BroadcastReceiver mStateReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(de.tubs.ibr.dtn.Intent.REGISTRATION))
			{
				Log.i(TAG, "registration successful");
				initializeSession(mRegistration);
			}
		}
	};
	
	private void initializeSession(Registration reg) {
		try {
			// do not initialize if the service is not connected
			if (mService == null) return;
			
			Session s = new Session(mService, mPackageName);
			
			s.initialize();
			
			synchronized(DTNClient.this) {
				mSession = s;
				notifyAll();
			}
			
			mSessionHandler.onSessionConnected(s);
		} catch (Exception e) {
	    	Intent registrationIntent = new Intent(de.tubs.ibr.dtn.Intent.REGISTER);
	    	registrationIntent.putExtra("app", PendingIntent.getBroadcast(mContext, 0, new Intent(), 0)); // boilerplate
	    	registrationIntent.putExtra("registration", (Parcelable)reg);
	    	mContext.startService(registrationIntent);
		}
	}
	
	private de.tubs.ibr.dtn.api.DTNSessionCallback mCallback = new de.tubs.ibr.dtn.api.DTNSessionCallback.Stub() {
		public void startBundle(Bundle bundle) throws RemoteException {
			if (mHandler == null) return;
			mHandler.startBundle(bundle);
		}

		public void endBundle() throws RemoteException {
			if (mHandler == null) return;
			mHandler.endBundle();
		}

{'fraction_non_alphanumeric': 0.09187088416279823, 'fraction_numerical': 0.002159127712404189, 'mean_word_length': 3.797514241325738, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5549136', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2629, 'n_words': 766}