org.xingjitong.LinphoneLauncherActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.xingjitong.LinphoneLauncherActivity.java

Source

/*
LinphoneLauncherActivity.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.xingjitong;

import static android.content.Intent.ACTION_MAIN;

import org.json.JSONException;
import org.wecarephone.R;
import org.xingjitong.compatibility.Compatibility;
import org.xingjitong.tang.set.HttpClientHelper;
import org.xingjitong.tang.set.UserConfig;

import com.umeng.message.PushAgent;
import com.umeng.message.proguard.C.e;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.telephony.PhoneNumberUtils;
import android.util.Config;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

/**
 * 
 * Launch Linphone main activity when Service is ready.
 * ??
 * @author Guillaume Beraudo
 * 
 */
public class LinphoneLauncherActivity extends Activity {

    private Handler mHandler;
    private ServiceWaitThread mThread;
    private SharedPreferences share;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        share = PreferenceManager.getDefaultSharedPreferences(this);
        Intent intent = getIntent();
        if (intent.getData() != null) {
            String num = PhoneNumberUtils.getNumberFromIntent(intent, this);
            share.edit().putBoolean(getString(R.string.pref_call_phone_type), true)
                    .putString(getString(R.string.pref_call_phone_num), num).commit();
        }
        boolean start = share.getBoolean(getString(R.string.pref_call_phone_type), false);
        if (start) {
            finish();
        }
        // Hack to avoid to draw twice LinphoneActivity on tablets
        //yyppexception del
        /*
        if (getResources().getBoolean(R.bool.isTablet)) {
           setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
           setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        */
        setContentView(R.layout.launcher);
        PushAgent.getInstance(this).onAppStart();
        //      try {
        //         PushAgent.getInstance(this).addAlias(UserConfig.getInstance().uid,"wecare");
        //      } catch (e e) {
        //         e.printStackTrace();
        //      } catch (JSONException e) {
        //         e.printStackTrace();
        //      }
        mHandler = new Handler();

        if (getResources().getBoolean(R.bool.enable_push_id)) {
            Compatibility.initPushNotificationService(this);
        }

        if (LinphoneService.isReady()) {
            onServiceReady();
        } else {
            // start linphone as background
            startService(new Intent(ACTION_MAIN).setClass(this, LinphoneService.class));
            mThread = new ServiceWaitThread();
            mThread.start();
        }
    }

    protected void onServiceReady() {
        LinphoneService.instance().setActivityToLaunchOnIncomingReceived(LinphoneActivity.class);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent().setClass(LinphoneLauncherActivity.this, LinphoneActivity.class)
                        .setData(getIntent().getData()));
                finish();
            }
        }, 60); //yyppnotice //200 //?200
    }

    private class ServiceWaitThread extends Thread {
        public void run() {
            while (!LinphoneService.isReady()) {
                try {
                    sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException("waiting thread sleep() has been interrupted");
                }
            }

            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    onServiceReady();
                }
            });
            mThread = null;
        }
    }
}