Code example for InvocationTargetException

Methods: w, getCause

0
	private void invokeMethod(Method method, Object[] args) {
		try { 
			mStartForeground.invoke(this, mStartForegroundArgs);
		} catch (InvocationTargetException e) {
			// Should not happen. 
			Log.w("ApiDemos", "Unable to invoke method", e);
		} catch (IllegalAccessException e) {
			// Should not happen. 
			Log.w("ApiDemos", "Unable to invoke method", e);
		} 
	} 
 
	/** 
	 * This is a wrapper around the new startForeground method, using the older 
	 * APIs if it is not available. 
	 */ 
	void startForegroundCompat(Notification notification) {
		// If we have the new startForeground API, then use it. 
		if (mStartForeground != null) {
			mStartForegroundArgs[0] = Integer.valueOf(NOTIFICATION_ID);
			mStartForegroundArgs[1] = notification;
			invokeMethod(mStartForeground, mStartForegroundArgs);
			return; 
		} 
 
		// Fall back on the old API. 
		mSetForegroundArgs[0] = Boolean.TRUE;
		invokeMethod(mSetForeground, mSetForegroundArgs);
		mNM.notify(NOTIFICATION_ID, notification);
	} 
 
	/** 
	 * This is a wrapper around the new stopForeground method, using the older 
	 * APIs if it is not available. 
	 */ 
	void stopForegroundCompat() { 
		// If we have the new stopForeground API, then use it. 
		if (mStopForeground != null) {
			mStopForegroundArgs[0] = Boolean.TRUE;
			try { 
				mStopForeground.invoke(this, mStopForegroundArgs);
			} catch (InvocationTargetException e) {
				// Should not happen. 
				Log.w("ApiDemos", "Unable to invoke stopForeground", e);
			} catch (IllegalAccessException e) {
				// Should not happen. 
				Log.w("ApiDemos", "Unable to invoke stopForeground", e);
			} 
			return; 
		} 
 
		// Fall back on the old API. Note to cancel BEFORE changing the 
		// foreground state, since we could be killed at that point. 
		mNM.cancel(NOTIFICATION_ID);
		mSetForegroundArgs[0] = Boolean.FALSE;
		invokeMethod(mSetForeground, mSetForegroundArgs);
	} 
 
	public synchronized void showNotification() { 
		if (songinfo != null) {
			isNotificationShowed = true;
			if (!isErrorOccuredDuringPlayback()) { 
				startForegroundCompat(createNotification(songinfo.artist, songinfo.title));
			} else { 
				startForegroundCompat(createNotification(getString(R.string.app_name), getString(R.string.alert_dialog_message_play_error)));
			} 
		} 
	} 
 
	public synchronized void hideNotification() { 
		isNotificationShowed = false;
		stopForegroundCompat(); 
	} 
 
	public void startPlaylist(String playlistName, List<SongInfo> playlist, int currentPlaylistPosition) {
		this.playlistName = playlistName;
		this.playlist = new LinkedList<SongInfo>(playlist);
		this.currentPlaylist = new LinkedList<SongInfo>(playlist);
		this.currentPlaylistPosition = 0;
		if (currentPlaylistPosition >= 0 && currentPlaylistPosition < this.currentPlaylist.size()) {
			this.currentPlaylistPosition = currentPlaylistPosition;
		} 
		songinfo = currentPlaylist.get(currentPlaylistPosition);
		if (shuffle) {
			Collections.shuffle(currentPlaylist);
		} 
		play(); 
	} 
 
	public boolean isRepeating() { 
		return repeat;
	} 
 
	public synchronized void setRepeat(boolean repeat) {
		this.repeat = repeat;
		if (player != null) {
			player.setLooping(repeat);
		} 
	} 
 
	public boolean isShuffling() { 
		return shuffle;
	} 
 
	public synchronized void setShuffle(boolean shuffle) {
		this.shuffle = shuffle;
		if (shuffle) {
			Collections.shuffle(currentPlaylist);
		} else { 
			this.currentPlaylist = new LinkedList<SongInfo>(playlist);
		} 
	} 
 
	public boolean isPlaying() { 
		if (player == null)
			return false; 
		return player.isPlaying();
	} 
 
	public synchronized void play() { 
		if (songinfo.id != -1) {
			if (player != null) {
				player.setOnCompletionListener(null);
				player.setOnPreparedListener(null);
				player.stop();
				player.release();
				player = null;
			} 
 
			try { 
				errorDuringPlayback = false;
				duration = 0;
				player = MediaPlayer.create(this, Uri.fromFile(Utils.getSongFile(songinfo.id)));
				player.setLooping(repeat);
				player.setOnCompletionListener(this);
				player.setOnPreparedListener(this);
				player.start();
				if (isNotificationShowed) {
					showNotification(); 
				} 
			} catch (Exception e) {
				Log.e(IMongolduuConstants.LOG_TAG, "exception", e);
				errorDuringPlayback = true;
				showNotification(); 
			} 
		} 
	} 
 
	public synchronized boolean isErrorOccuredDuringPlayback() { 
		return errorDuringPlayback;
	} 
 
	public synchronized void resume() { 
		if (player != null && !player.isPlaying()) {
			player.start();
		} 
	} 
 
	public synchronized boolean pause() { 
		if (player != null && player.isPlaying()) {
			player.pause();
			return true; 
		} 
		return false; 
	} 
 
	public synchronized void forward() { 
		currentPlaylistPosition++;
		if (currentPlaylistPosition >= 0 && currentPlaylistPosition < currentPlaylist.size()) {
			songinfo = currentPlaylist.get(currentPlaylistPosition);
			play(); 
		} else { 
			currentPlaylistPosition = 0;
			songinfo = currentPlaylist.get(currentPlaylistPosition);
			play(); 
		} 
	} 
 
	public synchronized void rewind() { 
		if (getCurrentPosition() > IMongolduuConstants.REWIND_REPLAY_CURRENT_POSITION) { 
			play(); 
		} else { 
			currentPlaylistPosition--;
			if (currentPlaylistPosition >= 0 && currentPlaylistPosition < currentPlaylist.size()) {
				songinfo = currentPlaylist.get(currentPlaylistPosition);
				play(); 
			} else { 
				currentPlaylistPosition = 0;
			} 
		} 
	} 
 
	public int getDuration() { 
		return duration / 1000;
	} 
 
	public synchronized int getCurrentPosition() { 
		if (player == null)
			return 0; 
		return player.getCurrentPosition() / 1000;
	} 
 
	public synchronized void seekTo(int sec) {
		if (player != null) {
			player.seekTo(sec * 1000);
		} 
	} 
	 
	public synchronized void phoneStateRinging() { 
		 
	} 
	 
	public synchronized void phoneStateIdle() { 
		 
	} 
 
	public SongInfo getSongInfo() { 
		return songinfo;
	} 
 
	public int getCurrentPlaylistPosition() { 
		return currentPlaylistPosition;
	} 
 
	public String getPlaylistName() {
		return playlistName;
	} 
 
	public Notification createNotification(String artistName, String songName) {
		Notification notification = new Notification(R.drawable.play1, artistName + " - " + songName, System.currentTimeMillis());
		PendingIntent intent = PendingIntent.getActivity(this, 0, Utils.createMediaPlayerIntent(this), 0);
		notification.setLatestEventInfo(getApplicationContext(), artistName, songName, intent);
		return notification;
	} 
 
	private static void initializeRemoteControlRegistrationMethods() { 
		try { 
			if (mRegisterMediaButtonEventReceiver == null) {
				mRegisterMediaButtonEventReceiver = AudioManager.class.getMethod("registerMediaButtonEventReceiver", new Class[] { ComponentName.class });
			} 
			if (mUnregisterMediaButtonEventReceiver == null) {
				mUnregisterMediaButtonEventReceiver = AudioManager.class.getMethod("unregisterMediaButtonEventReceiver", new Class[] { ComponentName.class });
			} 
		} catch (NoSuchMethodException nsme) {
 
		} 
	} 
 
	private void registerRemoteControl() { 
		try { 
			if (mRegisterMediaButtonEventReceiver == null) {
				return; 
			} 
			mRegisterMediaButtonEventReceiver.invoke(mAudioManager, mRemoteControlResponder);
		} catch (InvocationTargetException ite) {
			/* unpack original exception when possible */ 
			Throwable cause = ite.getCause();
			if (cause instanceof RuntimeException) {
				throw (RuntimeException) cause;
			} else if (cause instanceof Error) {
				throw (Error) cause;
			} else { 
				throw new RuntimeException(ite);
			} 
		} catch (IllegalAccessException ie) {
			Log.e(IMongolduuConstants.LOG_TAG, "exception", ie);
		} 
	} 
 
	private void unregisterRemoteControl() { 
		try { 
			if (mUnregisterMediaButtonEventReceiver == null) {
				return; 
			} 
			mUnregisterMediaButtonEventReceiver.invoke(mAudioManager, mRemoteControlResponder);
		} catch (InvocationTargetException ite) {
			/* unpack original exception when possible */ 
			Throwable cause = ite.getCause();
			if (cause instanceof RuntimeException) {
				throw (RuntimeException) cause;
			} else if (cause instanceof Error) {
				throw (Error) cause;
			} else {