<commit_before>/*
 * ServeStream: A HTTP stream browser/player for Android
 * Copyright 2010 <NAME>
 * 
 * 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 net.sourceforge.servestream;

import net.sourceforge.servestream.service.IMediaService;
import net.sourceforge.servestream.service.MediaService;
import net.sourceforge.servestream.utils.MediaFile;
import android.app.ListActivity;
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.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class NowPlayingActivity extends ListActivity {
	public final static String TAG = NowPlayingActivity.class.getName();
	
    private IMediaService mMediaService = null;
	private LayoutInflater mInflater = null;
    private NowPlayingAdapter mAdapter = null;
	
    private BroadcastReceiver mStatusListener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(MediaService.META_CHANGED) ||
            		action.equals(MediaService.META_UPDATED) ||
            			action.equals(MediaService.PLAYSTATE_CHANGED)) {
            	updateList();
            }
        }
    };
	
    private BroadcastReceiver mDisconnectListener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(MediaService.CLOSE_PLAYER)) {
            	finish();
            }
        }
    };
    
    private ServiceConnection connection = new ServiceConnection() {
        public void onServiceConnected(ComponentName classname, IBinder obj) {
	        // This is called when the connection with the service has been
	        // established, giving us the service object we can use to
	        // interact with the service.  Because we have bound to a explicit
	        // service that we know is running in our own process, we can
	        // cast its IBinder to a concrete class and directly access it.
            mMediaService = IMediaService.Stub.asInterface(obj);
            
            createList();
        }
        public void onServiceDisconnected(ComponentName classname) {
	        // This is called when the connection with the service has been
	        // unexpectedly disconnected -- that is, its process crashed.
	        // Because it is running in our same process, we should never
	        // see this happen.
            mMediaService = null;
        }
    };
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.act_nowplaying);

		this.setTitle(String.format("%s: %s",
				getResources().getText(R.string.app_name),
				getResources().getText(R.string.title_now_playing)));
		
		ListView list = this.getListView();
		list.setFastScrollEnabled(true);
		list.setOnItemClickListener(new OnItemClickListener() {
			public synchronized void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				try {
					if (mMediaService.getQueuePosition() == position) {
					    doPauseResume();
					} else {
					    mMediaService.setQueuePosition(position);
					}
					updateList();
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		});
		
		this.mInflater = LayoutInflater.from(this);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		// connect with manager service to find all bridges
		// when connected it will insert all views
		bindService(new Intent(this, MediaService.class), connection, Context.BIND_AUTO_CREATE);
		
        IntentFilter f = new IntentFilter();
        f.addAction(MediaService.PLAYSTATE_CHANGED);
        f.addAction(MediaService.META_CHANGED);
        f.addAction(MediaService.META_UPDATED);
        registerReceiver(mStatusListener, new IntentFilter(f));
        
        f = new IntentFilter();
        f.addAction(MediaService.CLOSE_PLAYER);
        registerReceiver(mDisconnectListener, new IntentFilter(f));
	}
	
	@Override
	public void onStop() {
		super.onStop();
		
        // Detach our existing connection.
        unbindService(connection);
        mMediaService = null;
        
        unregisterReceiver(mStatusListener);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		unregisterReceiver(mDisconnectListener);
	}
	
	protected void createList() {
		
		MediaFile[] streams = null;
		
		try {
			streams = mMediaService.getQueue();
		} catch (RemoteException e) {
			e.printStackTrace();
		}

		mAdapter = new NowPlayingAdapter(this, streams);
		
		this.setListAdapter(mAdapter);
	}
	
    private void doPauseResume() {
        try {
            if(mMediaService != null) {
                if (mMediaService.isPlaying()) {
                    mMediaService.pause();
                } else {
                    mMediaService.play();
                }
            }
        } catch (RemoteException ex) {
        }
    }
	
	private void updateList() {
		mAdapter.notifyDataSetChanged();
	}
	
	class NowPlayingAdapter extends ArrayAdapter<MediaFile> {<commit_msg>Added null check<commit_after>/*
 * ServeStream: A HTTP stream browser/player for Android
 * Copyright 2010 <NAME>
 * 
 * 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 net.sourceforge.servestream;

import net.sourceforge.servestream.service.IMediaService;
import net.sourceforge.servestream.service.MediaService;
import net.sourceforge.servestream.utils.MediaFile;
import android.app.ListActivity;
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.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class NowPlayingActivity extends ListActivity {
	public final static String TAG = NowPlayingActivity.class.getName();
	
    private IMediaService mMediaService = null;
	private LayoutInflater mInflater = null;
    private NowPlayingAdapter mAdapter = null;
	
    private BroadcastReceiver mStatusListener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(MediaService.META_CHANGED) ||
            		action.equals(MediaService.META_UPDATED) ||
            			action.equals(MediaService.PLAYSTATE_CHANGED)) {
            	updateList();
            }
        }
    };
	
    private BroadcastReceiver mDisconnectListener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(MediaService.CLOSE_PLAYER)) {
            	finish();
            }
        }
    };
    
    private ServiceConnection connection = new ServiceConnection() {
        public void onServiceConnected(ComponentName classname, IBinder obj) {
	        // This is called when the connection with the service has been
	        // established, giving us the service object we can use to
	        // interact with the service.  Because we have bound to a explicit
	        // service that we know is running in our own process, we can
	        // cast its IBinder to a concrete class and directly access it.
            mMediaService = IMediaService.Stub.asInterface(obj);
            
            createList();
        }
        public void onServiceDisconnected(ComponentName classname) {
	        // This is called when the connection with the service has been
	        // unexpectedly disconnected -- that is, its process crashed.
	        // Because it is running in our same process, we should never
	        // see this happen.
            mMediaService = null;
        }
    };
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.act_nowplaying);

		this.setTitle(String.format("%s: %s",
				getResources().getText(R.string.app_name),
				getResources().getText(R.string.title_now_playing)));
		
		ListView list = this.getListView();
		list.setFastScrollEnabled(true);
		list.setOnItemClickListener(new OnItemClickListener() {
			public synchronized void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				try {
					if (mMediaService.getQueuePosition() == position) {
					    doPauseResume();
					} else {
					    mMediaService.setQueuePosition(position);
					}
					updateList();
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		});
		
		this.mInflater = LayoutInflater.from(this);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		// connect with manager service to find all bridges
		// when connected it will insert all views
		bindService(new Intent(this, MediaService.class), connection, Context.BIND_AUTO_CREATE);
		
        IntentFilter f = new IntentFilter();
        f.addAction(MediaService.PLAYSTATE_CHANGED);
        f.addAction(MediaService.META_CHANGED);
        f.addAction(MediaService.META_UPDATED);
        registerReceiver(mStatusListener, new IntentFilter(f));
        
        f = new IntentFilter();
        f.addAction(MediaService.CLOSE_PLAYER);
        registerReceiver(mDisconnectListener, new IntentFilter(f));
	}
	
	@Override
	public void onStop() {
		super.onStop();
		
        // Detach our existing connection.
        unbindService(connection);
        mMediaService = null;
        
        unregisterReceiver(mStatusListener);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		unregisterReceiver(mDisconnectListener);
	}
	
	protected void createList() {
		
		MediaFile[] streams = null;
		
		try {
			streams = mMediaService.getQueue();
		} catch (RemoteException e) {
			e.printStackTrace();
		}

		mAdapter = new NowPlayingAdapter(this, streams);
		
		this.setListAdapter(mAdapter);
	}
	
    private void doPauseResume() {
        try {
            if(mMediaService != null) {
                if (mMediaService.isPlaying()) {
                    mMediaService.pause();
                } else {
                    mMediaService.play();
                }
            }
        } catch (RemoteException ex) {
        }
    }
	
	private void updateList() {
		if (mMediaService == null) {
			return;
		}
		
		mAdapter.notifyDataSetChanged();
	}
	
	class NowPlayingAdapter extends ArrayAdapter<MediaFile> {
{'fraction_non_alphanumeric': 0.08805133620339534, 'fraction_numerical': 0.0012996507188693038, 'mean_word_length': 2.933546325878594, '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': '932895', 'n_tokens_mistral': 3554, 'n_tokens_neox': 3194, 'n_words': 900}