linphone-android/src/org/linphone/LinphoneActivity.java

604 lines
18 KiB
Java

/*
LinphoneActivity.java
Copyright (C) 2010 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 static android.content.Intent.ACTION_MAIN;
import org.linphone.LinphoneManager.EcCalibrationListener;
import org.linphone.LinphoneManagerWaitHelper.LinphoneManagerReadyListener;
import org.linphone.LinphoneSimpleListener.LinphoneOnCallStateChangedListener;
import org.linphone.core.CallDirection;
import org.linphone.core.LinphoneCall;
import org.linphone.core.LinphoneCore;
import org.linphone.core.LinphoneCoreException;
import org.linphone.core.Log;
import org.linphone.core.LinphoneCall.State;
import org.linphone.core.LinphoneCore.EcCalibratorStatus;
import org.linphone.core.LinphoneCore.RegistrationState;
import org.linphone.mediastream.Version;
import org.linphone.mediastream.video.AndroidVideoWindowImpl;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.text.Html;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;
public class LinphoneActivity extends TabActivity implements
SensorEventListener
, ContactPicked
, LinphoneOnCallStateChangedListener
, LinphoneManagerReadyListener
{
public static final String DIALER_TAB = "dialer";
public static final String PREF_FIRST_LAUNCH = "pref_first_launch";
private static final int video_activity = 100;
static final int FIRST_LOGIN_ACTIVITY = 101;
static final int INCOMING_CALL_ACTIVITY = 103;
private static final int incall_activity = 104;
private static final String PREF_CHECK_CONFIG = "pref_check_config";
private static LinphoneActivity instance;
private PowerManager.WakeLock mWakeLock;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private int previousRotation = -1;
private Handler mHandler = new Handler();
// Customization
private static boolean useFirstLoginActivity;
private static boolean useMenuSettings;
private static boolean useMenuAbout;
private boolean checkAccount;
static final boolean isInstanciated() {
return instance != null;
}
public static final LinphoneActivity instance() {
if (instance != null) return instance;
throw new RuntimeException("LinphoneActivity not instantiated yet");
}
public void onCreate(Bundle savedInstanceState) {
instance = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinphonePreferenceManager.getInstance(this);
useFirstLoginActivity = getResources().getBoolean(R.bool.useFirstLoginActivity);
useMenuSettings = getResources().getBoolean(R.bool.useMenuSettings);
useMenuAbout = getResources().getBoolean(R.bool.useMenuAbout);
checkAccount = !useFirstLoginActivity;
// start linphone as background
startService(new Intent(ACTION_MAIN).setClass(this, LinphoneService.class));
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
if (!useFirstLoginActivity || pref.getBoolean(getString(R.string.first_launch_suceeded_once_key), false)) {
fillTabHost();
} else {
startActivityForResult(new Intent().setClass(this, FirstLoginActivity.class), FIRST_LOGIN_ACTIVITY);
}
if (checkAccount && !useFirstLoginActivity) {
if (pref.getBoolean(PREF_FIRST_LAUNCH, true)) {
onFirstLaunch();
} else if (!pref.getBoolean(PREF_CHECK_CONFIG, false)
&& !checkDefined(pref, R.string.pref_username_key, R.string.pref_domain_key)) {
onBadSettings(pref);
} else {
checkAccount = false;
}
}
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE,Log.TAG+"#"+getClass().getName());
LinphoneManager.addListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FIRST_LOGIN_ACTIVITY:
if (resultCode == RESULT_OK) {
Toast.makeText(this, getString(R.string.ec_calibration_launch_message), Toast.LENGTH_LONG).show();
try {
LinphoneManager.getInstance().startEcCalibration(new EcCalibrationListener() {
public void onEcCalibrationStatus(EcCalibratorStatus status, int delayMs) {
PreferenceManager.getDefaultSharedPreferences(LinphoneActivity.this)
.edit().putBoolean(
getString(R.string.pref_echo_canceller_calibration_key),
status == EcCalibratorStatus.Done).commit();
}
});
} catch (LinphoneCoreException e) {
Log.e(e, "Unable to calibrate EC");
}
fillTabHost();
} else {
finish();
stopService(new Intent(ACTION_MAIN).setClass(this, LinphoneService.class));
}
break;
case incall_activity:
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private synchronized void fillTabHost() {
if (((TabWidget) findViewById(android.R.id.tabs)).getChildCount() != 0) return;
startActivityInTab("history",
new Intent().setClass(this, HistoryActivity.class),
R.string.tab_history, R.drawable.history_orange);
startActivityInTab(DIALER_TAB,
new Intent().setClass(this, DialerActivity.class).setData(getIntent().getData()),
R.string.tab_dialer, R.drawable.dialer_orange);
startActivityInTab("contact",
new Intent().setClass(this, Version.sdkAboveOrEqual(5) ?
ContactPickerActivityNew.class : ContactPickerActivityOld.class),
R.string.tab_contact, R.drawable.contact_orange);
gotToDialer();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getData() == null) {
Log.e("LinphoneActivity received an intent without data, recreating GUI if needed");
if (!LinphoneService.isReady() || !LinphoneManager.getLc().isIncall()) return;
if(LinphoneManager.getLc().isInComingInvitePending()) {
gotToDialer();
} else {
startIncallActivity();
}
return;
}
if (DialerActivity.instance() != null) {
DialerActivity.instance().newOutgoingCall(intent);
} else {
Toast.makeText(this, getString(R.string.dialer_null_on_new_intent), Toast.LENGTH_LONG).show();
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
if (event==null || event.sensor == mAccelerometer) {
int rot;
rot = AndroidVideoWindowImpl.rotationToAngle(getWindowManager().getDefaultDisplay().getOrientation());
if (rot != previousRotation) {
Log.d("New device rotation: ", rot);
// Returning rotation FROM ITS NATURAL ORIENTATION
LinphoneCore lc=LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc!=null) lc.setDeviceRotation(rot);
//else ignore, we are probably exiting.
previousRotation = rot;
}
}
}
@Override
protected void onPause() {
super.onPause();
if (isFinishing()) {
//restore audio settings
boolean isUserRequest = false;
LinphoneManager.removeListener(this);
LinphoneManager.stopProximitySensorForActivity(this);
instance = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the currently selected menu XML resource.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.linphone_activity_menu, menu);
menu.findItem(R.id.menu_settings).setVisible(useMenuSettings);
menu.findItem(R.id.menu_about).setVisible(useMenuAbout);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc == null)
return true;
// hide settings menu when in call
// otherwise, exiting the 'setting' menu will cause exosip deletion/recreation...
menu.findItem(R.id.menu_settings).setVisible(!lc.isIncall());
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
startprefActivity();
return true;
case R.id.menu_exit:
finish();
stopService(new Intent(ACTION_MAIN)
.setClass(this, LinphoneService.class));
break;
case R.id.menu_about:
startActivity(new Intent(ACTION_MAIN)
.setClass(this, AboutActivity.class));
default:
Log.e("Unknown menu item [",item,"]");
break;
}
return false;
}
void startprefActivity() {
Intent intent = new Intent(ACTION_MAIN);
intent.setClass(this, LinphonePreferencesActivity.class);
startActivity(intent);
}
public synchronized void stopOrientationSensor() {
if (mSensorManager!=null)
mSensorManager.unregisterListener(this, mAccelerometer);
}
public synchronized void startOrientationSensor() {
if (mSensorManager!=null) {
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
/* init LC orientation value on orientation sensor startup */
previousRotation = -1;
onSensorChanged(null);
}
void showPreferenceErrorDialog(String message) {
if (!useMenuSettings) {
Toast.makeText(this, message, Toast.LENGTH_LONG);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage(String.format(getString(R.string.config_error), message))
.setCancelable(false)
.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startprefActivity();
}
})
.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create().show();
}
}
public void onRegistrationStateChanged(RegistrationState state,
String message) {
if (FirstLoginActivity.instance != null) {
FirstLoginActivity.instance.onRegistrationStateChanged(state);
}
}
/***** Check Account *******/
private boolean checkDefined(SharedPreferences pref, int ... keys) {
for (int key : keys) {
String conf = pref.getString(getString(key), null);
if (conf == null || "".equals(conf))
return false;
}
return true;
}
private void onFirstLaunch() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
TextView lDialogTextView = new TextView(this);
lDialogTextView.setAutoLinkMask(0x0f/*all*/);
lDialogTextView.setPadding(10, 10, 10, 10);
lDialogTextView.setText(Html.fromHtml(getString(R.string.first_launch_message)));
builder.setCustomTitle(lDialogTextView)
.setCancelable(false)
.setPositiveButton(getString(R.string.cont), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LinphoneManager.getInstance().initializePayloads();
startprefActivity();
checkAccount = false;
}
});
builder.create().show();
}
private void onBadSettings(final SharedPreferences pref) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
TextView lDialogTextView = new TextView(this);
lDialogTextView.setAutoLinkMask(0x0f/*all*/);
lDialogTextView.setPadding(10, 10, 10, 10);
lDialogTextView.setText(Html.fromHtml(getString(R.string.initial_config_error)));
builder.setCustomTitle(lDialogTextView)
.setCancelable(false)
.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startprefActivity();
checkAccount = false;
}
}).setNeutralButton(getString(R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
checkAccount = false;
}
}).setNegativeButton(getString(R.string.never_remind), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
pref.edit().putBoolean(PREF_CHECK_CONFIG, true).commit();
dialog.cancel();
checkAccount = false;
}
});
builder.create().show();
}
public void setAddressAndGoToDialer(String number, String name, Uri photo) {
DialerActivity.instance().setContactAddress(number, name, photo);
instance.gotToDialer();
}
private void gotToDialer() {
getTabHost().setCurrentTabByTag(DIALER_TAB);
}
private void startActivityInTab(String tag, Intent intent, int indicatorId, int drawableId) {
Drawable tabDrawable = getResources().getDrawable(drawableId);
TabSpec spec = getTabHost().newTabSpec(tag)
.setIndicator(getString(indicatorId), tabDrawable)
.setContent(intent);
getTabHost().addTab(spec);
}
// Do not call if video activity already launched as it would cause a pause() of the launched one
// and a race condition with capture surfaceview leading to a crash
public void startVideoActivity(LinphoneCall call, int delay) {
if (VideoCallActivity.launched || call == null) return;
call.enableCamera(true);
mHandler.postDelayed(new Runnable() {
public void run() {
if (VideoCallActivity.launched) return;
startActivityForResult(new Intent().setClass(
LinphoneActivity.this,
VideoCallActivity.class),
video_activity);
}
}, delay);
LinphoneManager.getInstance().routeAudioToSpeaker();
}
public void startIncallActivity() {
if (IncallActivity.active) {
return;
}
mHandler.post(new Runnable() {
public void run() {
startActivityForResult(new Intent().setClass(
LinphoneActivity.this,
IncallActivity.class),
incall_activity);
}
});
}
public void startIncomingCallActivity(LinphoneCall pendingCall) {
Intent intent = new Intent()
.setClass(this, IncomingCallActivity.class)
.putExtra("stringUri", pendingCall.getRemoteAddress().asStringUriOnly());
startActivityForResult(intent, INCOMING_CALL_ACTIVITY);
}
public void finishVideoActivity() {
mHandler.post(new Runnable() {
public void run() {
finishActivity(video_activity);
}
});
}
@Override
public void onCallStateChanged(LinphoneCall call, State state,
String message) {
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc==null) {
/* we are certainly exiting, ignore then.*/
return;
}
if (state==State.IncomingReceived) {
startIncomingCallActivity(call);
}
if (state == State.OutgoingInit) {
startIncallActivity();
}
if (state==State.OutgoingInit || state==State.IncomingReceived) {
startOrientationSensor();
enterIncallMode();
} else if (state==State.Error || state==State.CallEnd){
stopOrientationSensor();
finishActivity(INCOMING_CALL_ACTIVITY);
}
if (state==State.Connected) {
if (call.getDirection() == CallDirection.Incoming) {
startIncallActivity();
enterIncallMode();
}
}
if (state == LinphoneCall.State.StreamsRunning && Version.isVideoCapable()) {
boolean videoEnabled = call.getCurrentParamsCopy().getVideoEnabled();
if (videoEnabled) {
startVideoActivity(call, 1000);
} else {
finishVideoActivity();
}
}
if (state == LinphoneCall.State.CallUpdatedByRemote && Version.isVideoCapable()) {
if (VideoCallActivity.launched && !call.getCurrentParamsCopy().getVideoEnabled()) {
finishVideoActivity();
}
}
if (state==State.Error){
showToast(R.string.call_error, message);
if (lc.getCallsNb() == 0){
if (mWakeLock.isHeld()) mWakeLock.release();
exitCallMode();
}
}else if (state==State.CallEnd){
if (lc.getCallsNb() == 0){
exitCallMode();
}
}
}
private void showToast(int id, String txt) {
final String msg = String.format(getString(id), txt);
mHandler.post(new Runnable() {
public void run() {
Toast.makeText(LinphoneActivity.this, msg, Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onCreateWhenManagerReady() {}
private void enterIncallMode() {
LinphoneManager.startProximitySensorForActivity(this);
if (!mWakeLock.isHeld()) mWakeLock.acquire();
}
private void exitCallMode() {
finishActivity(incall_activity);
if (mWakeLock.isHeld()) mWakeLock.release();
mHandler.post(new Runnable() {
@Override
public void run() {
LinphoneManager.stopProximitySensorForActivity(LinphoneActivity.this);
}
});
setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);
}
@Override
public void onResumeWhenManagerReady() {
LinphoneCall pendingCall = LinphoneManager.getInstance().getPendingIncomingCall();
if (pendingCall != null) {
LinphoneActivity.instance().startIncomingCallActivity(pendingCall);
} else if (LinphoneManager.getLc().isIncall()) {
enterIncallMode();
}
if (LinphoneManager.getLc().getCallsNb() > 0) {
LinphoneManager.startProximitySensorForActivity(this);
// removing is done directly in LinphoneActivity.onPause()
}
}
@Override
protected void onDestroy() {
if (mWakeLock.isHeld()) mWakeLock.release();
super.onDestroy();
}
}
interface ContactPicked {
void setAddressAndGoToDialer(String number, String name, Uri photo);
}