org.xingjitong.ui.CallButton.java Source code

Java tutorial

Introduction

Here is the source code for org.xingjitong.ui.CallButton.java

Source

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

import org.linphone.core.CallDirection;
import org.linphone.core.LinphoneCallLog;
import org.linphone.core.LinphoneCoreException;
import org.linphone.core.LinphoneProxyConfig;
import org.wecarephone.R;
import org.xingjitong.App;
import org.xingjitong.LinphoneActivity;
import org.xingjitong.LinphoneManager;
import org.xingjitong.customdialog;
import org.xingjitong.tang.set.UserConfig;
import org.xingjitong.tang.set.source.NetWorkUtils;
import org.xingjitong.tang.set.source.SettingChangeUser;
import org.xingjitong.tang.set.source.SettingReg;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Guillaume Beraudo
 */
public class CallButton extends ImageView implements OnClickListener, AddressAware {

    private AddressText mAddress;
    private SharedPreferences share;
    private App app;
    private String phone;

    public void setAddressWidget(AddressText a) {
        this.mAddress = a;
    }

    public void setExternalClickListener(OnClickListener e) {
        setOnClickListener(e);
    }

    public void resetClickListener() {
        setOnClickListener(this);
    }

    public CallButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
        share = PreferenceManager.getDefaultSharedPreferences(context);
        if (LinphoneActivity.instance() != null)
            app = (App) LinphoneActivity.instance().getApplication();
    }

    //yyppdsp add
    private void toastdsp(String str) {

        //LayoutInflater inflater = getLayoutInflater();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View layout = inflater.inflate(R.layout.customtoast, (ViewGroup) findViewById(R.id.llcustomtoast));

        //ImageView image = (ImageView) layout.findViewById(R.id.customtoast_img);
        //image.setImageResource(R.drawable.status_green); //yypp

        TextView text = (TextView) layout.findViewById(R.id.customtoast_text);
        text.setText(str);
        //Toast toast = new Toast(getApplicationContext());
        Toast toast = new Toast(getContext());
        toast.setGravity(Gravity.CENTER, 0, 60);
        //toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();

    }

    //yyppdsp end   
    public void onClick(View v) {
        try {
            if (!LinphoneManager.getInstance().acceptCallIfIncomingPending()) {
                if (app != null)
                    phone = app.getCallNum();
                String toaststr = "";
                if (share.getInt(getContext().getString(R.string.setting_reg_uid), 0) == 0) {
                    toastdsp(",");
                    SettingReg reg = new SettingReg();
                    reg.setStyle(DialogFragment.STYLE_NO_TITLE, DialogFragment.STYLE_NORMAL);
                    reg.show(LinphoneActivity.instance().getSupportFragmentManager(), "reg");
                    return;
                }
                if (!NetWorkUtils.isNetWorkAvailable(getContext())) {
                    toastdsp("??,!");
                    return;
                }
                if (mAddress.getText().length() == 0 && !"".equals(phone) && phone != null) {
                    mAddress.getEditableText().insert(0, phone);
                    mAddress.setDisplayedName(phone);
                    return;
                }
                if (mAddress.getText().length() < 6) {
                    toastdsp("???!");
                    return;
                }
                if (!mAddress.getText().toString().startsWith("0") && !mAddress.getText().toString().startsWith("1")
                        && !mAddress.getText().toString().startsWith("+")) {
                    toastdsp("??!");
                    return;
                }
                //---------------------------------------------------------   
                if (mAddress.getText().length() > 0) {
                    if (UserConfig.getInstance().iswecarecall) {
                        if (app != null)
                            app.setCallNum(mAddress.getText().toString());

                        LinphoneManager.getInstance().newOutgoingCall(mAddress); //CallButton

                        UserConfig.getInstance().iswecarecall = false;
                    } else {

                        //Toast.makeText(getActivity(), "", Toast.LENGTH_LONG).show();
                        //?
                        if (app != null)
                            app.setCallNum(mAddress.getText().toString());

                        LinphoneManager.getInstance().newOutgoingCall(mAddress); //CallButton

                    }
                } else {
                    if (getContext().getResources().getBoolean(R.bool.call_last_log_if_adress_is_empty)) {
                        LinphoneCallLog[] logs = LinphoneManager.getLc().getCallLogs();
                        LinphoneCallLog log = null;
                        for (LinphoneCallLog l : logs) {
                            if (l.getDirection() == CallDirection.Outgoing) {
                                log = l;
                                break;
                            }
                        }
                        if (log == null) {
                            return;
                        }

                        LinphoneProxyConfig lpc = LinphoneManager.getLc().getDefaultProxyConfig();
                        if (lpc != null && log.getTo().getDomain().equals(lpc.getDomain())) {
                            mAddress.setText(log.getTo().getUserName());
                        } else {
                            mAddress.setText(log.getTo().asStringUriOnly());
                        }
                        mAddress.setSelection(mAddress.getText().toString().length());
                        mAddress.setDisplayedName(log.getTo().getDisplayName());
                    }
                }
            }
        } catch (LinphoneCoreException e) {
            LinphoneManager.getInstance().terminateCall();
            onWrongDestinationAddress();
        }
        ;
    }

    protected void onWrongDestinationAddress() {
        Toast.makeText(getContext(),
                String.format(getResources().getString(R.string.warning_wrong_destination_address),
                        mAddress.getText().toString()),
                Toast.LENGTH_LONG).show();
    }
}