Java tutorial
package org.xingjitong; /* DialerFragment.java Copyright (C) 2012 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. */ import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Map; import org.linphone.core.LinphoneCore; import org.linphone.core.Log; import org.wecarephone.R; import org.xingjitong.tang.db.CallInfor; import org.xingjitong.tang.db.CallsLogUtils; import org.xingjitong.tang.db.ContactDB; import org.xingjitong.tang.db.MSG; import org.xingjitong.tang.db.MSG.MSGEntity; import org.xingjitong.tang.set.HttpURLConnectionHelper; import org.xingjitong.tang.set.JsonToMapAds; import org.xingjitong.tang.set.UserConfig; import org.xingjitong.ui.AddressAware; import org.xingjitong.ui.AddressText; import org.xingjitong.ui.CallButton; import org.xingjitong.ui.EraseButton; import com.lidroid.xutils.BitmapUtils; import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; /** * @author Sylvain Berfini */ public class DialerFragment extends Fragment { private String urlString = "http://admin.wecare200.com/ad/advertisingplace/findByName?name=androidl"; private CirclePageIndicator indicator; private AutoScrollViewPager viewpager_dialer_ads; private List<View> list_ads = null; private static DialerFragment instance; private static boolean isCallTransferOngoing = false; public boolean mVisible; private AddressText mAddress; private CallButton mCall; private ImageView mAddContact, showInfor; private OnClickListener addContactListener, cancelListener, transferListener; private boolean shouldEmptyAddressField = true; private PopupWindow pop; private ListView listView; private String text, phone, phone2; private List<CallInfor> list; private int lBegin; private boolean start; private SharedPreferences share; private TextView msc; private TextView dialhint; // yyppcall add private Handler handler; private String gonglistr = null; private String nonglistr = null; private Lunar nongli; private MSG msg; private String adJson; private Handler handler2 = new Handler(); private List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>(); private View view; private OnItemClickedListener mylistener; public interface OnItemClickedListener { public void onClick(String info); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new Thread(new Runnable() { @Override public void run() { byte[] data = HttpURLConnectionHelper.loadByteFromURL(urlString); if (data != null && data.length > 0) { adJson = new String(data, 0, data.length); } if (adJson != null && adJson.length() > 0) { SharedPreferences prefs = getActivity().getSharedPreferences("ads", Context.MODE_PRIVATE); Editor editor = prefs.edit(); editor.putString("ads", adJson); editor.commit(); } } }).start(); } @SuppressWarnings("deprecation") @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { instance = this; handler = new Handler(); MSG.getInstance().setFragment(this); if (view == null) { view = inflater.inflate(R.layout.dialer, container, false); list_ads = new ArrayList<View>(); indicator = (CirclePageIndicator) view.findViewById(R.id.indicator); // ad view viewpager_dialer_ads = (AutoScrollViewPager) view.findViewById(R.id.viewpager_dialer_ads); final BitmapUtils utils = new BitmapUtils(getActivity(), "/sdcard/Download"); utils.configDefaultLoadFailedImage(R.drawable.default_ad); // image ads config final ImageView view1 = new ImageView(getActivity()); final ImageView view2 = new ImageView(getActivity()); final ImageView view3 = new ImageView(getActivity()); // view1.setImageResource(R.drawable.ad1); // view2.setImageResource(R.drawable.ad2); // view3.setImageResource(R.drawable.ad3); // final ImageView view4 = new ImageView(getActivity()); // final ImageView view5 = new ImageView(getActivity()); view1.setScaleType(ScaleType.FIT_XY); view2.setScaleType(ScaleType.FIT_XY); view3.setScaleType(ScaleType.FIT_XY); // view4.setScaleType(ScaleType.FIT_XY); // view5.setScaleType(ScaleType.FIT_XY); android.view.ViewGroup.LayoutParams params = new LayoutParams( android.view.ViewGroup.LayoutParams.MATCH_PARENT, 200); view1.setLayoutParams(params); view2.setLayoutParams(params); view3.setLayoutParams(params); // view4.setLayoutParams(params); // view5.setLayoutParams(params); SharedPreferences prefs = getActivity().getSharedPreferences("ads", Context.MODE_PRIVATE); String adsString = prefs.getString("ads", null); if (adsString != null && adsString.length() > 0) { Map<String, Object> map = JsonToMapAds.getAdsList(adsString); if (map != null) { @SuppressWarnings("unchecked") List<Map<String, Object>> list_pau = (List<Map<String, Object>>) map.get("list"); if (list_pau != null && list_pau.size() > 0) { list2 = list_pau; handler2.post(new Runnable() { @Override public void run() { if (list2 != null && list2.size() >= 3) { utils.display(view1, (String) list2.get(0).get("content")); utils.display(view2, (String) list2.get(1).get("content")); utils.display(view3, (String) list2.get(2).get("content")); // view1.setOnClickListener(new MyClickListener( // getActivity(), (String) list2 // .get(0).get("url"))); // view2.setOnClickListener(new MyClickListener( // getActivity(), (String) list2 // .get(1).get("url"))); // view3.setOnClickListener(new MyClickListener( // getActivity(), (String) list2 // .get(2).get("url"))); } else { view1.setImageResource(R.drawable.default_ad); view2.setImageResource(R.drawable.default_ad); view3.setImageResource(R.drawable.default_ad); } } }); } } } else { view1.setImageResource(R.drawable.default_ad); view2.setImageResource(R.drawable.default_ad); view3.setImageResource(R.drawable.default_ad); } list_ads.add(view1); list_ads.add(view2); list_ads.add(view3); // list_ads.add(view4); // list_ads.add(view5); viewpager_dialer_ads.setAdapter(new MyAdPagerAdapter(list_ads)); viewpager_dialer_ads.setOffscreenPageLimit(5); viewpager_dialer_ads.startAutoScroll(); viewpager_dialer_ads.setInterval(5000); viewpager_dialer_ads.setSlideBorderMode(2); indicator.setViewPager(viewpager_dialer_ads); mAddress = (AddressText) view.findViewById(R.id.Adress); mAddress.setDialerFragment(this); mAddress.requestFocus(); mAddress.setVisibility(View.VISIBLE); // yyppdial add dialhint = (TextView) view.findViewById(R.id.dialhint_tv); if (mAddress.length() == 0) { android.util.Log.v("yyyppdebug", "yyppdialaddress 000"); mAddress.setVisibility(View.GONE); dialhint.setVisibility(View.VISIBLE); } else { android.util.Log.v("yyyppdebug", "yyppdialaddress 111"); mAddress.requestFocus(); mAddress.setVisibility(View.VISIBLE); dialhint.setVisibility(View.GONE); } // yyppdial end // yypp add Calendar c = Calendar.getInstance(); // int month = c.get(Calendar.MONTH); // int date = c.get(Calendar.DATE); int week = c.get(Calendar.DAY_OF_WEEK); if (nongli == null) nongli = new Lunar(c); // nonglistr=nongli.toString(); gonglistr = nongli.togongliString() + " "; nonglistr = "" + nongli.tonongliString() + " "; android.util.Log.v("yyppdebug", "yyppnotice dial 000"); msg = MSG.getnewInstance(); // yyppnotice test android.util.Log.v("yyppdebug", "yyppnotice dialerfragment size:" + msg.msgs.size() + ""); int drawid[] = { /* * R.drawable.dial_notice0_img, R.drawable.dial_notice1_img, * R.drawable.dial_notice2_img, R.drawable.dial_notice3_img, * R.drawable.dial_notice4_img, R.drawable.dial_notice5_img, * R.drawable.dial_notice6_img */ }; // android.util.Log.v("yyppdebug", "yypp week="+week+""); /* * RelativeLayout rLayout = (RelativeLayout) * view.findViewById(R.id.dialer_notice_bg); Resources res = * getResources(); //resource handle Drawable drawable = * res.getDrawable(drawid[(week-1)%7]); //new Image that was added * to the res folder rLayout.setBackgroundDrawable(drawable); */ // yypp end share = PreferenceManager.getDefaultSharedPreferences(getActivity()); Bundle bundle = getArguments(); if (bundle != null && mAddress != null) { int lBegin = mAddress.getSelectionStart(); if (lBegin == -1) { lBegin = mAddress.length(); } phone = bundle.getString("phone"); } EraseButton erase = (EraseButton) view.findViewById(R.id.Erase); erase.setAddressWidget(mAddress); mCall = (CallButton) view.findViewById(R.id.Call); mCall.setAddressWidget(mAddress); if (LinphoneActivity.isInstanciated() && LinphoneManager.getLc().getCallsNb() > 0) { if (isCallTransferOngoing) { mCall.setImageResource(R.drawable.transfer_call); } else { mCall.setImageResource(R.drawable.add_call); } } else { mCall.setImageResource(R.drawable.call); } AddressAware numpad = (AddressAware) view.findViewById(R.id.Dialer); if (numpad != null) { numpad.setAddressWidget(mAddress); } addContactListener = new OnClickListener() { @Override public void onClick(View v) { //add person to contacts mylistener.onClick(mAddress.getText().toString()); LinphoneActivity.instance().displayContactsForEdition(mAddress.getText().toString()); } }; cancelListener = new OnClickListener() { @Override public void onClick(View v) { LinphoneActivity.instance().resetClassicMenuLayoutAndGoBackToCallIfStillRunning(); } }; transferListener = new OnClickListener() { @Override public void onClick(View v) { LinphoneCore lc = LinphoneManager.getLc(); if (lc.getCurrentCall() == null) { return; } lc.transferCall(lc.getCurrentCall(), mAddress.getText().toString()); LinphoneActivity.instance().resetClassicMenuLayoutAndGoBackToCallIfStillRunning(); isCallTransferOngoing = false; } }; mAddContact = (ImageView) view.findViewById(R.id.addContact); mAddContact .setEnabled(!(LinphoneActivity.isInstanciated() && LinphoneManager.getLc().getCallsNb() > 0)); resetLayout(isCallTransferOngoing); if (getArguments() != null) { shouldEmptyAddressField = false; String number = getArguments().getString("SipUri"); String displayName = getArguments().getString("DisplayName"); String photo = getArguments().getString("PhotoUri"); mAddress.setText(number); if (displayName != null) { mAddress.setDisplayedName(displayName); } if (photo != null) { mAddress.setPictureUri(Uri.parse(photo)); } } // MATCH_PARENT WRAP_CONTENT //yyppdial.test /* * pop = new PopupWindow((getActivity().getLayoutInflater().inflate( * R.layout.dialer_list, null)), * ViewGroup.LayoutParams.WRAP_CONTENT, * ViewGroup.LayoutParams.WRAP_CONTENT); */ pop = new PopupWindow((getActivity().getLayoutInflater().inflate(R.layout.dialer_list, null)), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); if (pop != null) { pop.setFocusable(true); pop.setBackgroundDrawable(new BitmapDrawable()); listView = (ListView) pop.getContentView().findViewById(R.id.dialer_pop_list); } if (listView != null) { listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { mAddress.setDisplayedName(list.get(arg2).num); mAddress.setText(list.get(arg2).num); pop.dismiss(); } }); } // SpannableString str=new SpannableString("1234567890"); // str.setSpan(new ForegroundColorSpan(Color.MAGENTA), 12, 15, // Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); showInfor = (ImageView) view.findViewById(R.id.dialer_infor_showimg1); if (showInfor != null) { showInfor.setOnClickListener(new OnClickListener() { @SuppressWarnings("unchecked") @Override public void onClick(View v) { if (pop.isShowing()) { pop.dismiss(); } else { list = new CallInfor().getCallInfor((List<Contact>) ContactDB.Instance().getAllOb()[2], CallsLogUtils.instance().getCallsLogs(), text); if (list.size() == 0) { return; } // yyyppset //yyppdal del listView.setAdapter(new DialerListViewAdapter(getActivity(), list, text)); // pop.setWidth(view.getWidth()); pop.setWidth(view.getWidth() - 70); pop.showAsDropDown(mAddress); // yyppcall //yyppset add /* * ListAdapter madapter=new * DialerListViewAdapter(getActivity(), list, text); * //listView.setAdapter(new * DialerListViewAdapter(getActivity(), list, * text)); listView.setAdapter(madapter); * * if (madapter.getCount()> 9) { * pop.setHeight(view.getHeight()-60); } else { * * pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT) * ; } pop.showAsDropDown(mAddress); */ // yyppcall end } } }); } String tmpstr1 = ""; if (UserConfig.getInstance().loginstatus == 0) tmpstr1 = ""; else tmpstr1 = ""; // /* * if (msc != null) { msc.setOnClickListener(new OnClickListener() { * * @Override public void onClick(View v) { SettingHelp mes = new * SettingHelp(); Bundle bundle = new Bundle(); * bundle.putInt("type", SettingHelp.NOTICE); * mes.setArguments(bundle); * mes.setStyle(DialogFragment.STYLE_NO_TITLE, * DialogFragment.STYLE_NORMAL); * mes.show(getActivity().getSupportFragmentManager(), "mes"); } }); * } */ // yyppdial end } ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) { parent.removeView(view); } // android.util.Log.i("xushuang", "<<<<2"+list2.toString()); // views for ads return view; } class MyClickListener implements OnClickListener { private String url; private Context context; public MyClickListener(Context context, String url) { this.context = context; this.url = url; } @Override public void onClick(View v) { Intent intent = new Intent(context, AdDetailActivity.class); Bundle bundle = new Bundle(); bundle.putString("url", url); intent.putExtras(bundle); context.startActivity(intent); } } // yyppoption add // yyppoption end /** * @return null if not ready yet */ public static DialerFragment instance() { return instance; } public void setEditText(String text) { this.text = text; android.util.Log.v("yyyppdebug", "setEditText 000"); if (text != null && !"".equals(text)) { // yyppdial add android.util.Log.v("yyyppdebug", "setEditText 111"); dialhint.setVisibility(View.GONE); mAddress.requestFocus(); mAddress.setVisibility(View.VISIBLE); // yyppdial end if (showInfor != null) { // yyppdial.test add list = new CallInfor().getCallInfor((List<Contact>) ContactDB.Instance().getAllOb()[2], CallsLogUtils.instance().getCallsLogs(), text); if (list.size() > 0) { // //yyppdial.test end showInfor.setVisibility(View.VISIBLE); } else { showInfor.setVisibility(View.GONE); // //yyppdial.test add } } if (msc != null) { // yyppdial del // msc.setVisibility(View.GONE); } if (mAddress != null) { // yyppcall del // mAddress.setVisibility(View.VISIBLE); } } else {// && list.size() > 0 // yyppdial add android.util.Log.v("yyyppdebug", "setEditText 333"); dialhint.setVisibility(View.VISIBLE); mAddress.setVisibility(View.GONE); // yyppdial end if (showInfor != null) { showInfor.setVisibility(View.GONE);// yyppdial//yyppinfo del } if (msc != null) { msc.setVisibility(View.VISIBLE); } if (mAddress != null) { // yyppcall del // mAddress.setVisibility(View.GONE); } } } public void setTextView() { android.util.Log.v("yyppdebug", "yypp settextview 000"); if (msc != null) { handler.post(new Runnable() { @Override public void run() { List<MSGEntity> msgs = MSG.getInstance().msgs; String tex = ""; for (MSGEntity msg : msgs) { if (msg.content == null || "".equals(msg.content)) { continue; } tex = tex + msg.content + "\t\t\t"; } // android.util.Log.v("yyppdebug", "yypp settextview 111"); if (tex != null && !"".equals(tex)) { msc.setVisibility(View.VISIBLE); msc.setText(tex); msc.requestFocus(); // yyppdial del // yyppdial del // mAddress.setVisibility(View.GONE); } // yyppdial add else { msc.setVisibility(View.VISIBLE); String tmpstr1 = ""; if (UserConfig.getInstance().loginstatus == 0) tmpstr1 = ""; else tmpstr1 = ""; // msc.setText("?:" + tmpstr1 + "(" + UserConfig.getInstance().loginlevel + ")"); msc.requestFocus(); // yyppdial del } // yyppdial end } }); } } @Override public void onAttach(Activity activity) { super.onAttach(activity); if (LinphoneActivity.isInstanciated()) { LinphoneActivity.instance().updateDialerFragment(this); } if (getActivity() instanceof OnItemClickedListener) { mylistener = (OnItemClickedListener) getActivity(); } } @SuppressLint("NewApi") @Override public void onResume() { super.onResume(); // mAddress.setText(""); //yyppdial del List<MSGEntity> msgs = MSG.getInstance().msgs; String tex = ""; for (MSGEntity msg : msgs) { if (msg.content == null || "".equals(msg.content)) { continue; } tex = tex + msg.content + "\t\t\t"; } if (msc != null && tex != null && !"".equals(tex)) { msc.setVisibility(View.VISIBLE); msc.setText(tex); msc.requestFocus(); // yyppdial del // mAddress.setVisibility(View.GONE); } if (LinphoneActivity.isInstanciated()) { LinphoneActivity.instance().selectMenu(FragmentsAvailable.DIALER); LinphoneActivity.instance().updateDialerFragment(this); } if (shouldEmptyAddressField) { mAddress.setText(""); } else { shouldEmptyAddressField = true; } resetLayout(isCallTransferOngoing); showInfor.setVisibility(View.GONE); // yyppdial//yyppinfo del if (lBegin >= 0 && !"".equals(phone)) { mAddress.setText(phone); // mAddress.getEditableText().insert(lBegin, String.valueOf(phone)); } start = share.getBoolean(getString(R.string.pref_call_phone_type), false); phone2 = share.getString(getString(R.string.pref_call_phone_num), null); if (start && !"".equals(phone2)) { mAddress.setText(phone2); share.edit().putBoolean(getString(R.string.pref_call_phone_type), false) .putString(getString(R.string.pref_call_phone_num), "").commit(); start = false; mCall.onClick(mCall); } } public void resetLayout(boolean callTransfer) { isCallTransferOngoing = callTransfer; LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); if (lc == null) { return; } if (lc.getCallsNb() > 0) { if (isCallTransferOngoing) { mCall.setImageResource(R.drawable.transfer_call); mCall.setExternalClickListener(transferListener); } else { mCall.setImageResource(R.drawable.add_call); mCall.resetClickListener(); } mAddContact.setEnabled(true); mAddContact.setImageResource(R.drawable.cancel); mAddContact.setOnClickListener(cancelListener); } else { mCall.setImageResource(R.drawable.call); mAddContact.setEnabled(true); mAddContact.setImageResource(R.drawable.add_contact); mAddContact.setOnClickListener(addContactListener); enableDisableAddContact(); } } public void enableDisableAddContact() { if (mAddContact != null) { mAddContact.setEnabled( LinphoneManager.getLc().getCallsNb() > 0 || !mAddress.getText().toString().equals("")); } } public void newOutgoingCall(Intent intent) { if (intent != null && intent.getData() != null) { String scheme = intent.getData().getScheme(); if (scheme.startsWith("imto")) { mAddress.setText("sip:" + intent.getData().getLastPathSegment()); } else if (scheme.startsWith("call") || scheme.startsWith("sip")) { mAddress.setText(intent.getData().getSchemeSpecificPart()); } else { Log.e("Unknown scheme: ", scheme); mAddress.setText(intent.getData().getSchemeSpecificPart()); } // yyppdialtest // mAddress.clearDisplayedName(); intent.setData(null); LinphoneManager.getInstance().newOutgoingCall(mAddress); } } }