package dokgol.nugu; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import android.app.AlertDialog; import android.app.Dialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Vibrator; import android.telephony.PhoneNumberUtils; import android.telephony.PhoneStateListener; import android.telephony.SmsMessage; import android.telephony.TelephonyManager; import android.text.Html; import android.text.Spanned; import android.util.Log; import android.widget.Button; import android.widget.Toast; import android.widget.TextView; import java.sql.Date; import java.text.SimpleDateFormat; import java.util.Locale; import java.util.Random; public class Receiver extends BroadcastReceiver { private final String httpurl = "http://www.goodsweb.kr/nugu/api/"; private final String dbName = "webnautes"; private final String tableName = "person"; SQLiteDatabase sampleDB = null; public static final String TAG = "PHONE STATE"; private static String mLastState; private final Handler mHandler = new Handler(Looper.getMainLooper()); private String LOG_TAG = "CheckCall"; private static int pState = TelephonyManager.CALL_STATE_IDLE; private MediaPlayer mp; int phonenum = 0; @Override public void onReceive(final Context context, Intent intent) { Log.d(TAG,"onReceive()"); sampleDB = context.openOrCreateDatabase(dbName, context.MODE_PRIVATE, null); try { String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if (state.equals(mLastState)) { return; } else { mLastState = state; } } catch (Exception e) { } String action = intent.getAction(); Bundle bundle = intent.getExtras(); //Toast.makeText(context, "PHONE ½ÃÀÛ", Toast.LENGTH_SHORT).show(); try { TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); telManager.listen(new PhoneStateListener(){ public void onCallStateChanged(int state, String incomingNumber){ if(state != pState){ if(state == TelephonyManager.CALL_STATE_RINGING){ //ÆùÀ̿︰´Ù try { /* sampleDB.execSQL("INSERT INTO " + tableName + " (xphone, xdate, xtype) " + "Values (" + "'"+reftelc+"'," + "DATE()," + "'"+refst+"');"); */ //Toast.makeText(context, "°É·Á¿Â ÀüÈ­ ["+incomingNumber+"] ", Toast.LENGTH_SHORT).show(); Random rnd = new Random(); int num = rnd.nextInt(100); phonenum = num; sampleDB.execSQL("INSERT INTO " + tableName + " (xleno, xphone, xdate, xtype) " + "Values (" +"'"+num+"'," + "'"+incomingNumber+"'," + "DATE()," + "'3');"); //strPhoneNumber //°É·Á¿Â ÀüÈ­ Á¤º¸ â ¶ç¿ì±â PostSend2(context, "2",""+incomingNumber); //String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); // final String phone_number = PhoneNumberUtils.formatNumber(incomingNumber); /* ¼ö½Å : Y -> ÀüÈ­ ¹ÞÀ¸¸é [2] * * */ //Toast.makeText(context, "["+phonenum+"]¼ö½Å [1] " +incomingNumber, Toast.LENGTH_SHORT).show(); } catch (Exception e) { } try { //Intent serviceIntent = new Intent(context, CallingService.class); //serviceIntent.putExtra("popdoc", incomingNumber); //context.startService(serviceIntent); } catch (Exception e) { } // Toast.makeText(context, "[RINGING] " +incomingNumber, Toast.LENGTH_SHORT).show(); } if(state == TelephonyManager.CALL_STATE_OFFHOOK){ //ÆùÀ̿︰´Ù //phonenum = phonenum + 1; /* ¹ß½Å¶§ [Y] , */ if(phonenum > 0 ){ //°É·Á¿Â ÀüÈ­ //Toast.makeText(context, "°É·Á¿Â ÀüÈ­ ¹ÞÀ½ [" +incomingNumber+"]", Toast.LENGTH_SHORT).show(); sampleDB.execSQL("UPDATE " + tableName + " SET xtype='1' where xleno='"+phonenum+"' and xtype='3'"); } else { //³»°¡ °Ç ÀüÈ­ //Toast.makeText(context, "³»°¡ °Ç ÀüÈ­ ["+incomingNumber+"] ", Toast.LENGTH_SHORT).show(); /* sampleDB.execSQL("INSERT INTO " + tableName + " (xphone, xdate, xtype) " + "Values (" + "'"+incomingNumber+"'," + "DATE()," + "'1');"); */ } //PostSend2(context, "2",""+incomingNumber); } pState = state; } } }, PhoneStateListener.LISTEN_CALL_STATE); } catch (Exception e) { } } /* * CALL_STATE_OFFHOOK ÅëÈ­½ÃÀÛ * */ //»óÅ üũ public void PostSend2(Context context,String fstyle, String ftelc){ // Toast.makeText(context, ""+ "key1="+fnum+"&key2="+fdoc, Toast.LENGTH_SHORT).show(); //Toast.makeText(context, ""+ "["+fstyle+"]ÀüÈ­±â·Ï µÇ¾ú½À´Ï´Ù.", Toast.LENGTH_SHORT).show(); String strPhoneNumber = ""; try { //TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE); //String phoneNum = telephonyManager.getLine1Number(); TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); strPhoneNumber = telManager.getLine1Number(); if(strPhoneNumber.startsWith("+82")){ strPhoneNumber = strPhoneNumber.replace("+82", "0"); } } catch (Exception e){} try { //¿äûdata ¶Ç´Â ¿äûXML //URLEncoder.encode("key1", "UTF-8") String data = "key1="+ strPhoneNumber +"&"; data += "key2="+ fstyle +"&"; data += "key3="+ ftelc; //data = URLEncoder.encode(data, "UTF-8"); URL url = new URL(httpurl+"app_online_phone_sql.php"); //URL url = new URL("http://www.callmeshop.net/sms.update.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; StringBuilder strRead = new StringBuilder(); while ((line = rd.readLine()) != null) { strRead.append(line); //°á°ú ¾ò¾î¿À±â } String data_strRead = strRead.toString().trim(); if(data_strRead.length() > 0) { String[] spdata = data_strRead.split("\\|"); String outtop_type = spdata[0].toString(); String outtop_fst = spdata[1].toString(); String outtop_ftelc = spdata[2].toString(); String outtop_doc = spdata[3].toString(); if(outtop_type.toString().trim().equals("Y")) { try { //outtop_doc = outtop_doc.toString().replace(" ", "\u00A0"); Intent serviceIntent = new Intent(context, CallingService.class); serviceIntent.putExtra("fst", outtop_fst); serviceIntent.putExtra("ftelc", outtop_ftelc); serviceIntent.putExtra("popdoc", outtop_doc); context.startService(serviceIntent); } catch (Exception e) { } //Toast.makeText(context, ""+ "["+fstyle+"]ÀüÈ­±â·Ï µÇ¾ú½À´Ï´Ù.", Toast.LENGTH_SHORT).show(); } } wr.close(); rd.close(); } catch (Exception e) { } } public static String dateFormat(long $timeMillis, String $pattern) { SimpleDateFormat format = new SimpleDateFormat($pattern, Locale.getDefault()); return format.format($timeMillis); } }