Guest User

Untitled

a guest
Oct 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.18 KB | None | 0 0
  1. package com.chrudgames.stayintouch;
  2.  
  3.  
  4. import java.sql.Date;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import com.google.ads.*;
  8. import com.google.ads.AdRequest.ErrorCode;
  9. import android.app.Activity;
  10. import android.app.AlarmManager;
  11. import android.app.AlertDialog;
  12. import android.app.ListActivity;
  13. import android.app.PendingIntent;
  14. import android.content.Context;
  15. import android.content.DialogInterface;
  16. import android.content.Intent;
  17. import android.content.SharedPreferences;
  18. import android.database.Cursor;
  19. import android.graphics.drawable.Drawable;
  20. import android.net.Uri;
  21. import android.os.Bundle;
  22. import android.preference.PreferenceManager;
  23. import android.provider.ContactsContract;
  24. import android.provider.ContactsContract.CommonDataKinds.Phone;
  25. import android.provider.ContactsContract.Contacts;
  26. import android.provider.ContactsContract.Contacts.Data;
  27. import android.util.Log;
  28. import android.view.Menu;
  29. import android.view.MenuInflater;
  30. import android.view.MenuItem;
  31. import android.view.View;
  32. import android.widget.Button;
  33. import android.widget.LinearLayout;
  34. import android.widget.ListView;
  35. import android.widget.ProgressBar;
  36. import android.widget.SimpleCursorAdapter;
  37. import android.widget.TextView;
  38. import android.widget.SimpleCursorAdapter.ViewBinder;
  39.  
  40.  
  41.  
  42.  
  43.  
  44. public class StayInTouchActivity extends ListActivity implements AdListener {
  45.  
  46. private AdView adView;
  47.  
  48.  
  49.  
  50. public interface AdListener {
  51. public void onReceiveAd(Ad ad);
  52. public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode error);
  53. public void onPresentScreen(Ad ad);
  54. public void onDismissScreen(Ad ad);
  55. public void onLeaveApplication(Ad ad);
  56. }
  57.  
  58. private static final int CONTACT_PICKER_RESULT = 1001;
  59.  
  60. boolean NotifyPreference;
  61. String DurationPreference;
  62. int listIndex;
  63. boolean bolOwned;
  64. int intContacts;
  65. int arrContacts[];
  66. SimpleCursorAdapter adapter;
  67. private static Context mContext;
  68.  
  69.  
  70.  
  71. /** Called when the activity is first created. */
  72. @Override
  73. public void onCreate(Bundle savedInstanceState) {
  74. super.onCreate(savedInstanceState);
  75. setContentView(R.layout.main);
  76. mContext = this;
  77.  
  78. // ListView listview=(ListView)findViewById(android.R.id.list);
  79. //listview.setOnScrollListener(onLstScroll);
  80.  
  81. //Create an offset from the current time in which the alarm will go off.
  82. Calendar cal = Calendar.getInstance();
  83. cal.add(Calendar.HOUR, 24 );
  84. //Create a new PendingIntent and add it to the AlarmManager
  85. Intent intent = new Intent(this, AlarmReceiverActivity.class);
  86. PendingIntent pendingIntent = PendingIntent.getActivity(this,
  87. 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  88. AlarmManager am =
  89. (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
  90. am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
  91. pendingIntent);
  92.  
  93.  
  94. DatabaseHandler db = new DatabaseHandler(this);
  95. bolOwned = db.owned();
  96. db.close();
  97. //bolOwned=true;
  98.  
  99.  
  100. if(bolOwned==false){
  101.  
  102. // Create the adView
  103. adView = new AdView(this, AdSize.BANNER, "a14fa9a02c91beb");
  104.  
  105. // Lookup your LinearLayout assuming it’s been given
  106. // the attribute android:id="@+id/mainLayout"
  107. LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout1);
  108.  
  109. // Add the adView to it
  110. layout.addView(adView);
  111.  
  112. // Initiate a generic request to load it with an ad
  113.  
  114. AdRequest adRequest = new AdRequest();
  115. //adRequest.addTestDevice(AdRequest.TEST_EMULATOR); // Emulator
  116. //adRequest.addTestDevice("HT097HL04633");
  117.  
  118. adView.setAdListener(this);
  119. adView.loadAd(adRequest);
  120. }else{
  121.  
  122.  
  123.  
  124. }
  125.  
  126. }
  127.  
  128.  
  129.  
  130. @Override
  131. public void onReceiveAd(Ad ad){
  132. fillList();
  133. }
  134.  
  135. @Override
  136. public void onDestroy() {
  137. if (adView != null) {
  138. adView.destroy();
  139. }
  140. super.onDestroy();
  141. }
  142.  
  143.  
  144. /*
  145.  
  146. OnScrollListener onLstScroll= new OnScrollListener()
  147. {
  148. @Override
  149. public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
  150. {
  151.  
  152.  
  153. }
  154.  
  155. @Override
  156. public void onScrollStateChanged(AbsListView view, int scrollState)
  157. {
  158.  
  159. switch (scrollState) {
  160.  
  161.  
  162. case OnScrollListener.SCROLL_STATE_IDLE:
  163. //adapter.notifyDataSetChanged();
  164. break;
  165. case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
  166. //adapter.notifyDataSetChanged();
  167. break;
  168. case OnScrollListener.SCROLL_STATE_FLING:
  169. //adapter.notifyDataSetChanged();
  170. break;
  171.  
  172. }
  173.  
  174. }
  175. };
  176.  
  177. */
  178.  
  179.  
  180. @Override
  181. public boolean onCreateOptionsMenu(Menu menu) {
  182. MenuInflater inflater = getMenuInflater();
  183. inflater.inflate(R.layout.menu, menu);
  184.  
  185. if(bolOwned){
  186. MenuItem i = menu.findItem(R.id.upgrade);
  187. i.setVisible(false);
  188. }
  189.  
  190. return true;
  191. }
  192.  
  193. private void getPrefs() {
  194.  
  195. SharedPreferences prefs = PreferenceManager
  196. .getDefaultSharedPreferences(getBaseContext());
  197. NotifyPreference = prefs.getBoolean("pref_notify", true);
  198. DurationPreference = prefs.getString("pref_duration", "60");
  199. if(DurationPreference.length()<1){DurationPreference="0";}
  200.  
  201. }
  202.  
  203.  
  204.  
  205. @Override
  206. public void onResume()
  207. {
  208. DatabaseHandler db = new DatabaseHandler(this);
  209. bolOwned = db.owned();
  210. db.close();
  211. // bolOwned=true;
  212.  
  213. super.onResume();
  214. }
  215.  
  216.  
  217. @Override
  218. public boolean onOptionsItemSelected(MenuItem item) {
  219. // Handle item selection
  220. switch (item.getItemId()) {
  221. case R.id.options:
  222. Intent i = new Intent(this, Prefs.class);
  223. startActivity(i);
  224.  
  225. return true;
  226. case R.id.help:
  227. Intent i2 = new Intent(this, Help.class);
  228. startActivity(i2);
  229. return true;
  230. case R.id.upgrade:
  231. Intent i3 = new Intent(this, Upgrade.class);
  232. startActivity(i3);
  233. return true;
  234. default:
  235. return super.onOptionsItemSelected(item);
  236. }
  237. }
  238.  
  239. @Override
  240. public void onWindowFocusChanged(boolean hasFocus) {
  241.  
  242. if(hasFocus){
  243.  
  244. getPrefs();
  245. updateDays();
  246. fillList();
  247.  
  248. }
  249. }
  250.  
  251.  
  252.  
  253.  
  254. public void fillList() {
  255.  
  256. try{
  257.  
  258. DatabaseHandler db = new DatabaseHandler(this);
  259.  
  260. String[] fields = new String[] { "name", "frequency", "dDays", "precMax", "precMax", "precMax", "last" };
  261. int[] views = new int[] { R.id.textView1, R.id.textView3, R.id.textView2, R.id.progressBar1, R.id.progressBar2, R.id.progressBar3 };
  262. Cursor c = (Cursor) db.getAllContactsCursor();
  263.  
  264. // Log.w("onActivityResult", "Count=" + c.getCount());
  265.  
  266. //startManagingCursor(c);
  267.  
  268. arrContacts=new int[c.getCount()];
  269. int i=0;
  270.  
  271. c.moveToFirst();
  272. while (c.isAfterLast() == false)
  273. {
  274. arrContacts[i] = c.getInt(7);
  275.  
  276.  
  277. i++;
  278. c.moveToNext();
  279. }
  280.  
  281. TextView mTextView = (TextView) findViewById(R.id.tvInit);
  282.  
  283. if(c.getCount()>0){
  284. mTextView.setVisibility(View.GONE);
  285.  
  286. intContacts = c.getCount();
  287.  
  288.  
  289. Button btn = (Button)findViewById(R.id.btnadd);
  290.  
  291. if(bolOwned==false && intContacts>4){
  292. btn.setText(this.getString(R.string.Upgrade));
  293. Drawable img = this.getResources().getDrawable( R.drawable.happy_bot_small);
  294. btn.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
  295.  
  296. }else{
  297. btn.setText(this.getString(R.string.Add_Contact));
  298. Drawable img = this.getResources().getDrawable( R.drawable.ic_input_add);
  299. btn.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
  300.  
  301. }
  302. }else{
  303. mTextView.setVisibility(View.VISIBLE);
  304.  
  305. }
  306.  
  307. adapter = new SimpleCursorAdapter(
  308. this,
  309. R.layout.main_row_view,
  310. c, fields, views);
  311.  
  312. adapter.setViewBinder(new ProductViewBinder());
  313.  
  314. setListAdapter(adapter);
  315.  
  316.  
  317. db.close();
  318.  
  319. // c.close();
  320.  
  321.  
  322. }catch (Error e){
  323.  
  324. alertbox("Oops!", "There was a problem: " + e.getMessage());
  325.  
  326. }
  327.  
  328. }
  329.  
  330.  
  331. private static class ProductViewBinder implements ViewBinder {
  332.  
  333. public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
  334.  
  335.  
  336. if (view.getId()==R.id.textView2) {
  337.  
  338.  
  339. if(cursor.getString(2).contentEquals("1800-01-01")){
  340.  
  341. ((TextView)view).setText(mContext.getString(R.string.No_Calls));
  342.  
  343. }else{
  344.  
  345. ((TextView)view).setText(cursor.getString(6).toString());
  346. }
  347.  
  348. return true;
  349.  
  350. }else if (view instanceof ProgressBar) {
  351. // String result = cursor.getString(2);
  352. int resInt = cursor.getInt(7);
  353.  
  354.  
  355. if(resInt <= 90) {
  356. if(view.getId()==R.id.progressBar1){
  357.  
  358.  
  359. ((ProgressBar)view).setVisibility(View.VISIBLE);
  360.  
  361. }else{
  362. ((ProgressBar)view).setVisibility(View.INVISIBLE);
  363. }
  364. //d = view.getResources().getDrawable(R.drawable.progress_green);
  365. //((ProgressBar)view).setProgress(resInt);
  366. } else {
  367.  
  368. if(resInt <= 100) {
  369. if(view.getId()==R.id.progressBar3){
  370. ((ProgressBar)view).setVisibility(View.VISIBLE);
  371.  
  372. }else{
  373. ((ProgressBar)view).setVisibility(View.INVISIBLE);
  374. }
  375. }else{
  376. if(view.getId()==R.id.progressBar2){
  377. ((ProgressBar)view).setVisibility(View.VISIBLE);
  378.  
  379. }else{
  380. ((ProgressBar)view).setVisibility(View.INVISIBLE);
  381. }
  382. }
  383.  
  384. }
  385.  
  386.  
  387. ((ProgressBar)view).setProgress(resInt);
  388.  
  389. return true;
  390.  
  391. }
  392.  
  393. return false;
  394. }
  395. }
  396.  
  397.  
  398. public void doLaunchContactPicker(View view) {
  399. if(bolOwned||intContacts<5){
  400. Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
  401. Contacts.CONTENT_URI);
  402. startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
  403. } else {
  404.  
  405. Intent i = new Intent(this, Upgrade.class);
  406. startActivity(i);
  407.  
  408. }
  409.  
  410. }
  411.  
  412.  
  413.  
  414. protected void onListItemClick(ListView l, View v, int position, long id) {
  415.  
  416. //DatabaseHandler db = new DatabaseHandler(this);
  417. //db.runQuery("delete from contacts where name = 'Jenny'");
  418. //db.close();
  419.  
  420. super.onListItemClick(l, v, position, id);
  421. //Object o = this.getListAdapter().getItem(position);
  422.  
  423. Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
  424. c.moveToPosition(position);
  425.  
  426. final String lookupKey = c.getString(0);
  427. final int intFrequency = c.getInt(1);
  428. final String strPhone = c.getString(4);
  429.  
  430.  
  431. c.close();
  432. l = null;
  433.  
  434. Intent i = new Intent(this, Detail.class);
  435. Bundle b1 = new Bundle();
  436.  
  437. b1.putString("lookupKey", lookupKey);
  438. b1.putInt("frequency", intFrequency);
  439. b1.putString("phone", strPhone);
  440.  
  441. i.putExtras(b1);
  442.  
  443. startActivity(i);
  444. }
  445.  
  446.  
  447. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  448. if (resultCode == RESULT_OK) {
  449.  
  450.  
  451.  
  452.  
  453. switch (requestCode) {
  454.  
  455. case CONTACT_PICKER_RESULT:
  456. Uri result = data.getData();
  457. String id = result.getLastPathSegment();
  458.  
  459. /*
  460. Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI
  461. , new String[]{ Phone.NUMBER
  462. }
  463. , Phone.CONTACT_ID + "=? AND " + Data.MIMETYPE + "=? AND " + Data.IS_PRIMARY + "=1"
  464. , new String[]{ String.valueOf(id),Phone.CONTENT_ITEM_TYPE }, null);
  465. if(c.moveToFirst()){
  466. */
  467.  
  468.  
  469. Cursor cursor = getContentResolver().query(
  470. Phone.CONTENT_URI, null,
  471. Phone.CONTACT_ID + "=? ",
  472. new String[]{id}, Data.IS_PRIMARY + " desc");
  473.  
  474.  
  475.  
  476.  
  477. if (cursor.moveToFirst()) {
  478. int phoneIdx = cursor.getColumnIndex(Phone.DATA);
  479. String phone = cursor.getString(phoneIdx);
  480.  
  481. phoneIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
  482. String name = cursor.getString(phoneIdx);
  483.  
  484. phoneIdx = cursor.getColumnIndex(Phone.LOOKUP_KEY);
  485. String lookup = cursor.getString(phoneIdx);
  486.  
  487. phoneIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
  488. int photo = cursor.getInt(phoneIdx);
  489.  
  490.  
  491. if (phone.length() == 0) {
  492.  
  493. alertbox("No Number", "There are no phone numbers listed for this contact, please add at least one and try again.");
  494. }
  495.  
  496. else {
  497.  
  498.  
  499. DatabaseHandler db = new DatabaseHandler(this);
  500.  
  501. Contact contact = new Contact();
  502.  
  503. contact._frequency = 14;
  504. contact._id = lookup;
  505. contact._last = Date.valueOf("1800-01-01");
  506. contact._name = name;
  507. contact._phone_number = phone;
  508. contact._photo = photo;
  509.  
  510. db.addContact(contact);
  511.  
  512.  
  513. db.close();
  514.  
  515. //db.deleteContact(contact);
  516.  
  517. //Log.w("onActivityResult", "Count=" + db.getContactsCount());
  518.  
  519. }
  520.  
  521.  
  522. Intent i = new Intent(this, Setup.class);
  523. Bundle b1 = new Bundle();
  524.  
  525. b1.putString("lookupKey", lookup);
  526.  
  527. i.putExtras(b1);
  528.  
  529.  
  530.  
  531. startActivity(i);
  532.  
  533. //break;
  534.  
  535. }else{
  536.  
  537. alertbox("No Number", "There are no phone numbers listed for this contact, please add at least one and try again.");
  538.  
  539.  
  540. }
  541.  
  542. cursor.close();
  543.  
  544.  
  545.  
  546. break;
  547. }
  548.  
  549. } else {
  550. // gracefully handle failure
  551.  
  552. }
  553. }
  554.  
  555. public void updateDays(){
  556.  
  557. //try {
  558.  
  559. String lookupKey = "";
  560. String tmpPhone = "";
  561. String phoneNumbers = "";
  562. String strName = "";
  563.  
  564. final String[] projection = { "date", "duration", "type" };
  565. String selection = "duration>" + DurationPreference;
  566. final String selectionArgs[] = null;// phoneNumbers;
  567. final String sortOrder = android.provider.CallLog.Calls.DATE + " DESC";
  568.  
  569. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  570.  
  571. DatabaseHandler db = new DatabaseHandler(this);
  572. Cursor contacts = (Cursor) db.getAllContactsCursor();
  573.  
  574. while (contacts.moveToNext()) {
  575.  
  576.  
  577.  
  578.  
  579. if (contacts.getString(0).equalsIgnoreCase(db.getID().toString())) {
  580.  
  581. }else{
  582.  
  583.  
  584. lookupKey = contacts.getString(0);
  585.  
  586. Log.d("SIT", "Key=" + lookupKey);
  587.  
  588. Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey);
  589.  
  590.  
  591. Cursor c = getContentResolver().query(lookupUri, new String[]{Contacts.DISPLAY_NAME, Contacts.LAST_TIME_CONTACTED, Contacts.HAS_PHONE_NUMBER},null, null, null);
  592.  
  593. if (c.getCount()==0){
  594. Log.d("Error", "Count=0");
  595. phoneNumbers="";
  596. db.runQuery("delete from contacts where _id='" + lookupKey + "'");
  597. alertbox("Missing Contact", contacts.getString(3) + " is no longer in your people directory and has been removed from StayInTouch");
  598. }else{
  599.  
  600.  
  601. c.moveToFirst();
  602.  
  603. String hasPhone = c.getString(2);
  604. strName = c.getString(0);
  605.  
  606. if (hasPhone.contains("1")) {
  607.  
  608. Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY +" = '" + lookupKey + "'", null, null);
  609. while (phones.moveToNext()) {
  610.  
  611. tmpPhone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  612. tmpPhone = tmpPhone.replaceAll("[^0-9]","");
  613.  
  614. if(tmpPhone.length()>6){tmpPhone = tmpPhone.substring(tmpPhone.length() - 6);}
  615.  
  616. phoneNumbers = phoneNumbers + "'" + tmpPhone + "',";
  617.  
  618. }
  619. phones.close();
  620.  
  621. // Log.w("onActivityResult", "Phones: " + c.getString(0).toString() + phoneNumbers);
  622.  
  623. if(phoneNumbers.length()>1){ phoneNumbers = phoneNumbers.substring(0, phoneNumbers.length() - 1);}
  624.  
  625. }
  626. }
  627.  
  628.  
  629.  
  630. c.close();
  631.  
  632.  
  633. selection = "duration>" + DurationPreference + " and substr(replace(number, '-',''),-6,6) IN (" + phoneNumbers + ")";
  634.  
  635. Cursor calldate = null;
  636.  
  637.  
  638. Long datLatest = null;
  639.  
  640.  
  641. calldate = getContentResolver().query(
  642. Uri.parse("content://call_log/calls"),
  643. projection,
  644. selection,
  645. selectionArgs,
  646. sortOrder);
  647.  
  648.  
  649. if (calldate.moveToFirst()) {
  650.  
  651. datLatest = calldate.getLong(0);
  652.  
  653.  
  654.  
  655. db.runQuery("update contacts set last = '" + dateFormat.format(datLatest) + "', name='" + strName + "' where _id='" + lookupKey.toString() + "' and " +
  656. " datetime(last) < Datetime('" + dateFormat.format(datLatest) + "')");
  657.  
  658. }
  659.  
  660. calldate.close();
  661.  
  662. phoneNumbers = "";
  663.  
  664.  
  665. //db.close();
  666. }
  667.  
  668. }
  669.  
  670. contacts.close();
  671. db.close();
  672.  
  673. //} catch (Exception e) {
  674.  
  675. // Log.d("SIT", "error=" + e.getMessage());
  676.  
  677. // }
  678.  
  679. }
  680.  
  681. protected void alertbox(String title, String mymessage)
  682. {
  683. new AlertDialog.Builder(this)
  684. .setMessage(mymessage)
  685. .setTitle(title)
  686. .setCancelable(true)
  687. .setNeutralButton(android.R.string.ok,
  688. new DialogInterface.OnClickListener() {
  689. public void onClick(DialogInterface dialog, int whichButton){}
  690. })
  691. .show();
  692. }
  693.  
  694. @Override
  695. public void onDismissScreen(Ad arg0) {
  696. // TODO Auto-generated method stub
  697.  
  698. }
  699.  
  700. @Override
  701. public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
  702. // TODO Auto-generated method stub
  703.  
  704. }
  705.  
  706. @Override
  707. public void onLeaveApplication(Ad arg0) {
  708. // TODO Auto-generated method stub
  709.  
  710. }
  711.  
  712. @Override
  713. public void onPresentScreen(Ad arg0) {
  714. // TODO Auto-generated method stub
  715.  
  716. }
  717.  
  718.  
  719.  
  720. }
Add Comment
Please, Sign In to add comment