diff --git a/res/layout/wait_service_dialog.xml b/res/layout/wait_service_dialog.xml new file mode 100644 index 000000000..e69c00152 --- /dev/null +++ b/res/layout/wait_service_dialog.xml @@ -0,0 +1,8 @@ + + + + + diff --git a/src/org/linphone/DialerActivity.java b/src/org/linphone/DialerActivity.java index 9b0b7b165..f1d46ba9b 100644 --- a/src/org/linphone/DialerActivity.java +++ b/src/org/linphone/DialerActivity.java @@ -62,7 +62,7 @@ import android.widget.Toast; * * */ -public class DialerActivity extends SoftVolumeActivity implements LinphoneGuiListener, NewOutgoingCallUiListener { +public class DialerActivity extends LinphoneManagerWaitActivity implements LinphoneGuiListener, NewOutgoingCallUiListener { private TextView mStatus; private View mHangup; @@ -89,7 +89,7 @@ public class DialerActivity extends SoftVolumeActivity implements LinphoneGuiLis private static final String CURRENT_ADDRESS = "org.linphone.current-address"; private static final String CURRENT_DISPLAYNAME = "org.linphone.current-displayname"; - private static final int INCOMING_CALL_DIALOG_ID = 1; + private static final int incomingCallDialogId = 1; /** * @return null if not ready yet @@ -146,19 +146,20 @@ public class DialerActivity extends SoftVolumeActivity implements LinphoneGuiLis checkIfOutgoingCallIntentReceived(); - if (LinphoneService.isReady()) { - LinphoneCore lc = LinphoneManager.getLc(); - if (lc.isIncall()) { - if(lc.isInComingInvitePending()) { - callPending(lc.getCurrentCall()); - } else { - enterIncallMode(lc); - } - } - } instance = this; } + @Override + protected void onLinphoneManagerAvailable(LinphoneManager m) { + LinphoneCore lc = LinphoneManager.getLc(); + if (lc.isIncall()) { + if(lc.isInComingInvitePending()) { + callPending(lc.getCurrentCall()); + } else { + enterIncallMode(lc); + } + } + } @@ -268,7 +269,7 @@ public class DialerActivity extends SoftVolumeActivity implements LinphoneGuiLis private void exitCallMode() { // Remove dialog if existing try { - dismissDialog(INCOMING_CALL_DIALOG_ID); + dismissDialog(incomingCallDialogId); } catch (Throwable e) {/* Exception if never created */} if (useIncallActivity) { @@ -301,50 +302,57 @@ public class DialerActivity extends SoftVolumeActivity implements LinphoneGuiLis private void callPending(final LinphoneCall call) { - showDialog(INCOMING_CALL_DIALOG_ID); + showDialog(incomingCallDialogId); } @Override protected void onPrepareDialog(int id, Dialog dialog) { + if (id == incomingCallDialogId) { String from = LinphoneManager.getInstance().extractIncomingRemoteName(); String msg = String.format(getString(R.string.incoming_call_dialog_title), from); ((AlertDialog) dialog).setMessage(msg); + } else { super.onPrepareDialog(id, dialog); + } } @Override protected Dialog onCreateDialog(int id) { - View incomingCallView = getLayoutInflater().inflate(R.layout.incoming_call, null); + if (id == incomingCallDialogId) { + View incomingCallView = getLayoutInflater().inflate(R.layout.incoming_call, null); - final Dialog dialog = new AlertDialog.Builder(this) - .setMessage("") - .setCancelable(false) - .setView(incomingCallView).create(); - - - ((CallButton) incomingCallView.findViewById(R.id.Call)).setExternalClickListener(new OnClickListener() { - public void onClick(View v) { - dialog.dismiss(); - if (Version.isVideoCapable()) { - LinphoneManager.getInstance().resetCameraFromPreferences(); - - // Privacy setting to not share the user camera by default - boolean prefVideoEnable = LinphoneManager.getInstance().isVideoEnabled(); - int key = R.string.pref_video_automatically_share_my_video_key; - boolean prefAutoShareMyCamera = mPref.getBoolean(getString(key), false); - boolean videoMuted = !(prefVideoEnable && prefAutoShareMyCamera); - - LinphoneManager.getLc().getCurrentCall().enableCamera(prefAutoShareMyCamera); + final Dialog dialog = new AlertDialog.Builder(this) + .setMessage("") + .setCancelable(false) + .setView(incomingCallView).create(); + + + ((CallButton) incomingCallView.findViewById(R.id.Call)).setExternalClickListener(new OnClickListener() { + public void onClick(View v) { + dialog.dismiss(); + if (Version.isVideoCapable()) { + LinphoneManager.getInstance().resetCameraFromPreferences(); + + // Privacy setting to not share the user camera by default + boolean prefVideoEnable = LinphoneManager.getInstance().isVideoEnabled(); + int key = R.string.pref_video_automatically_share_my_video_key; + boolean prefAutoShareMyCamera = mPref.getBoolean(getString(key), false); + boolean videoMuted = !(prefVideoEnable && prefAutoShareMyCamera); + + LinphoneManager.getLc().getCurrentCall().enableCamera(prefAutoShareMyCamera); + } } - } - }); - ((HangCallButton) incomingCallView.findViewById(R.id.Decline)).setExternalClickListener(new OnClickListener() { - public void onClick(View v) {dialog.dismiss();} - }); - - return dialog; + }); + ((HangCallButton) incomingCallView.findViewById(R.id.Decline)).setExternalClickListener(new OnClickListener() { + public void onClick(View v) {dialog.dismiss();} + }); + + return dialog; + } else { + return super.onCreateDialog(id); + } } - + public void newOutgoingCall(Intent intent) { diff --git a/src/org/linphone/LinphoneManagerWaitActivity.java b/src/org/linphone/LinphoneManagerWaitActivity.java new file mode 100644 index 000000000..3899aefb3 --- /dev/null +++ b/src/org/linphone/LinphoneManagerWaitActivity.java @@ -0,0 +1,103 @@ +/* +LinphoneManagerWaitActivity.java +Copyright (C) 2011 Belledonne Communications, Grenoble, France + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ +package org.linphone; + +import org.linphone.core.Log; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.os.Bundle; +import android.os.Handler; +import android.view.View; + +/** + * Activity requiring access to LinphoneManager should inherit from this class. + * + * @author Guillaume Beraudo + * + */ +public abstract class LinphoneManagerWaitActivity extends SoftVolumeActivity { + + private final int waitServiceDialogId = 314159265; + private Handler mHandler = new Handler(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (LinphoneService.isReady()) { + onLinphoneManagerAvailable(LinphoneManager.getInstance()); + } else { + showDialog(waitServiceDialogId); + thread = new ServiceWaitThread(); + thread.start(); + } + } + + private ServiceWaitThread thread; + + @Override + protected void onDestroy() { + if (thread != null) thread.interrupt(); + super.onDestroy(); + } + + @Override + protected Dialog onCreateDialog(int id) { + if (id == waitServiceDialogId) { + View v = getLayoutInflater().inflate((R.layout.wait_service_dialog), null); + return new AlertDialog.Builder(this).setView(v).setCancelable(false).create(); + } + return super.onCreateDialog(id); + } + + protected abstract void onLinphoneManagerAvailable(LinphoneManager m); + + private void dismissDialogFromThread(final int id) { + mHandler.post(new Runnable() { + @Override + public void run() { + try { + dismissDialog(id); + } catch (Throwable e) { + // Discarding exception which may be thrown if the dialog wasn't showing. + } + } + }); + } + + private class ServiceWaitThread extends Thread { + @Override + public void run() { + while (!LinphoneService.isReady()) { + try { + sleep(30); + } catch (InterruptedException e) { + Log.e("waiting thread sleep() has been interrupted, exiting as requested"); + dismissDialogFromThread(waitServiceDialogId); // FIXME, may not be the best thing to do + return; + } + } + onLinphoneManagerAvailable(LinphoneManager.getInstance()); + dismissDialogFromThread(waitServiceDialogId); + super.run(); + } + } + +} diff --git a/src/org/linphone/LinphoneService.java b/src/org/linphone/LinphoneService.java index ce75cf533..9220f9f3e 100644 --- a/src/org/linphone/LinphoneService.java +++ b/src/org/linphone/LinphoneService.java @@ -99,7 +99,6 @@ public final class LinphoneService extends Service implements LinphoneServiceLis @Override public void onCreate() { super.onCreate(); - instance = this; // In case restart after a crash. Main in LinphoneActivity LinphonePreferenceManager.getInstance(this); @@ -127,6 +126,7 @@ public final class LinphoneService extends Service implements LinphoneServiceLis LinphoneManager.createAndStart(this, this); + instance = this; // instance is ready once linphone manager has been created } diff --git a/src/org/linphone/SoftVolumeActivity.java b/src/org/linphone/SoftVolumeActivity.java index 8f8d6dddb..af3cb5b25 100644 --- a/src/org/linphone/SoftVolumeActivity.java +++ b/src/org/linphone/SoftVolumeActivity.java @@ -18,6 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package org.linphone; +import org.linphone.core.Log; import org.linphone.mediastream.video.capture.hwconf.Hacks; import android.app.Activity; @@ -35,9 +36,12 @@ public class SoftVolumeActivity extends Activity { public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) - && (Hacks.needSoftvolume() || LinphonePreferenceManager.getInstance().useSoftvolume())) { + && (Hacks.needSoftvolume() || LinphonePreferenceManager.getInstance(this).useSoftvolume())) { - if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { + if (!LinphoneService.isReady()) { + Log.i("Couldn't change softvolume has service is not running"); + return true; + } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { LinphoneManager.getInstance().adjustSoftwareVolume(1); } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { LinphoneManager.getInstance().adjustSoftwareVolume(-1);