Advertisement
jumpy83

ListaContatti

Nov 11th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. package it.fontanagianpaolo.www.project;
  2.  
  3. import android.app.Dialog;
  4. import android.app.ListActivity;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.database.Cursor;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.telephony.PhoneStateListener;
  12. import android.telephony.TelephonyManager;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.widget.ArrayAdapter;
  16. import android.widget.Button;
  17. import android.widget.ListView;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. import java.util.ArrayList;
  22. import java.util.List;
  23.  
  24. import it.fontanagianpaolo.www.project.database.PersonaHelper;
  25. import it.fontanagianpaolo.www.project.database.PersonaManager;
  26. import it.fontanagianpaolo.www.project.model.Persona;
  27.  
  28.  
  29. public class ListaContatti extends ListActivity {
  30.  
  31. PersonaManager manager;
  32. List<Persona> lista = new ArrayList<>();
  33. TextView visualizza;
  34. private PersonaHelper mPersonaHelper;
  35. private static final String TAG = "MyActivity";
  36.  
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_lista_contatti);
  42. mPersonaHelper = new PersonaHelper(this);
  43. manager = new PersonaManager(this);
  44. manager.open();
  45. visualizzaLista();
  46. //stampaCose();
  47. manager.close();
  48.  
  49. final ArrayAdapter<Persona> adapter = new ArrayAdapter<>(this, R.layout.row, R.id.textViewList, lista.toArray(new Persona[0]));
  50.  
  51. setListAdapter(adapter);
  52.  
  53. }
  54.  
  55. protected void onListItemClick(ListView list, View v, int position, long id) {
  56.  
  57. final Dialog dialog = new Dialog(this);
  58. dialog.setTitle("Continua con ");
  59. dialog.setContentView(R.layout.custom_dialog_layout);
  60.  
  61. TextView view = (TextView)findViewById(R.id.textViewList);
  62.  
  63. final Button call = (Button) dialog.findViewById(R.id.call);
  64. Button sms = (Button) dialog.findViewById(R.id.sms);
  65. //Button social = (Button) dialog.findViewById(R.id.social);
  66.  
  67. // add PhoneStateListener
  68. PhoneCallListener phoneListener = new PhoneCallListener();
  69. TelephonyManager telephonyManager = (TelephonyManager) this
  70. .getSystemService(Context.TELEPHONY_SERVICE);
  71. telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
  72.  
  73. call.setOnClickListener(new View.OnClickListener() {
  74. @Override
  75. public void onClick(View view) {
  76.  
  77.  
  78. SQLiteDatabase db = mPersonaHelper.getReadableDatabase();
  79. // final Cursor cursor = db.rawQuery("SELECT TELEFONO FROM contatti", null);
  80.  
  81. final Cursor cursor = db.query("contatti",new String[]{"TELEFONO"},null,null,null,null,null);
  82. cursor.moveToFirst();
  83.  
  84. final int numeroTelefono = cursor.getColumnIndex(PersonaHelper.TELEFONO);
  85.  
  86. while (cursor.isAfterLast() == false) {
  87. //view.append("n" + cursor.getString(numeroTelefono));
  88. cursor.moveToNext();
  89. }
  90.  
  91. Intent callIntent = new Intent(Intent.ACTION_CALL);
  92. callIntent.setData(Uri.parse("tel:" + numeroTelefono ));
  93. //callIntent.setData(Uri.parse("tel" + db.query("contatti", new String[]{"TELEFONO"}, null, null, null, null, null)));
  94. startActivity(callIntent);
  95. }
  96. });
  97.  
  98. sms.setOnClickListener(new View.OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. try {
  102.  
  103. Intent sendIntent = new Intent(Intent.ACTION_VIEW);
  104. sendIntent.putExtra("sms_body", "default content");
  105. sendIntent.setType("vnd.android-dir/mms-sms");
  106. startActivity(sendIntent);
  107.  
  108. } catch (Exception e) {
  109. Toast.makeText(getApplicationContext(),
  110. "SMS faild, please try again later!",
  111. Toast.LENGTH_LONG).show();
  112. e.printStackTrace();
  113. }
  114. }
  115. });
  116.  
  117. dialog.show();
  118. }
  119.  
  120.  
  121. private void stampaCose() {
  122. StringBuilder sb = new StringBuilder();
  123. for (Persona attuale : lista) {
  124. sb.append(attuale.toString());
  125. sb.append("\n");
  126. }
  127. }
  128. private void visualizzaLista() {
  129. Cursor cursor = manager.getPersoneSalvate();
  130. lista.clear();
  131. cursor.moveToFirst();
  132. while (!cursor.isAfterLast()) {
  133.  
  134. String cognome = cursor.getString(cursor.getColumnIndex(PersonaHelper.COGNOME));
  135. String nome = cursor.getString(cursor.getColumnIndex(PersonaHelper.NOME));
  136. String ruolo = cursor.getString(cursor.getColumnIndex(PersonaHelper.RUOLO));
  137. String numero = cursor.getString(cursor.getColumnIndex(PersonaHelper.TELEFONO));
  138. //String mail = cursor.getString(cursor.getColumnIndex(PersonaHelper.COLUMN_MAIL));
  139. // String descrizione = cursor.getString(cursor.getColumnIndex(PersonaHelper.COLUMN_DESCRIZIONE));
  140.  
  141. //lista.add(new Persona(cognome,nome, numero, mail)); // al posto dei due null mettere mail e descrizione
  142. lista.add(new Persona(cognome, nome, ruolo, numero));
  143.  
  144. cursor.moveToNext();
  145. }
  146.  
  147. }
  148.  
  149. //monitor phone call activities
  150. private class PhoneCallListener extends PhoneStateListener {
  151.  
  152. private boolean isPhoneCalling = false;
  153.  
  154. String LOG_TAG = "LOGGING 123";
  155.  
  156. @Override
  157. public void onCallStateChanged(int state, String incomingNumber) {
  158.  
  159. if (TelephonyManager.CALL_STATE_RINGING == state) {
  160. // phone ringing
  161. Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
  162. }
  163.  
  164. if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
  165. // active
  166. Log.i(LOG_TAG, "OFFHOOK");
  167.  
  168. isPhoneCalling = true;
  169. }
  170.  
  171. if (TelephonyManager.CALL_STATE_IDLE == state) {
  172. // run when class initial and phone call ended,
  173. // need detect flag from CALL_STATE_OFFHOOK
  174. Log.i(LOG_TAG, "IDLE");
  175.  
  176. if (isPhoneCalling) {
  177.  
  178. Log.i(LOG_TAG, "restart app");
  179.  
  180. // restart app
  181. Intent i = getBaseContext().getPackageManager()
  182. .getLaunchIntentForPackage(
  183. getBaseContext().getPackageName());
  184. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  185. startActivity(i);
  186.  
  187. isPhoneCalling = false;
  188. }
  189.  
  190. }
  191. }
  192. }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement