1
1
package com .mycontacts ;
2
2
3
+ import java .io .File ;
4
+ import java .io .FileNotFoundException ;
5
+ import java .io .FileOutputStream ;
6
+ import java .io .IOException ;
7
+ import java .io .InputStream ;
8
+ import java .io .OutputStream ;
3
9
import java .util .ArrayList ;
10
+ import java .util .Date ;
4
11
import java .util .Random ;
5
12
6
13
import android .net .Uri ;
12
19
import android .annotation .SuppressLint ;
13
20
import android .app .Activity ;
14
21
import android .content .ContentResolver ;
22
+ import android .content .SharedPreferences ;
15
23
import android .database .Cursor ;
24
+ import android .text .format .DateFormat ;
25
+ import android .text .method .DateTimeKeyListener ;
16
26
import android .util .Log ;
17
27
import android .view .Menu ;
18
28
import android .view .MenuItem ;
26
36
public class DisplayContacts extends Activity {
27
37
private EditText filterText = null ;
28
38
public ArrayList <Boolean > itemChecked = null ;
29
-
30
-
39
+
40
+ final boolean tempvar =false ;
41
+ int k =0 ;
42
+ String d =null ;
31
43
ArrayAdapter <String > myAdapterInstance ;
32
44
SimpleCursorAdapter adapter ;
33
45
String [] projection = new String [] {
@@ -41,7 +53,22 @@ public class DisplayContacts extends Activity {
41
53
public void onCreate (Bundle savedInstanceState ) {
42
54
super .onCreate (savedInstanceState );
43
55
setContentView (R .layout .displaycontacts );
44
-
56
+ try {
57
+ String destpath = "/data/data/" + getPackageName ()
58
+ + "/databases/dbContacts" ;
59
+ File f = new File (destpath );
60
+ if (!f .exists ()) {
61
+ copyDB (getBaseContext ().getAssets ().open ("dbContacts" ),
62
+ new FileOutputStream (destpath ));
63
+ }
64
+ } catch (FileNotFoundException e ) {
65
+ e .printStackTrace ();
66
+ } catch (IOException e ) {
67
+ e .printStackTrace ();
68
+ }
69
+
70
+ SQLHelper con = new SQLHelper (this );
71
+
45
72
//Find the random number between 1 to 5
46
73
Random r = new Random ();
47
74
int rndcnt =r .nextInt (5 )+1 ;
@@ -56,34 +83,81 @@ public void onCreate(Bundle savedInstanceState) {
56
83
String name []=new String [size ];
57
84
String number []=new String [size ];
58
85
Log .e ("hi" ,"before for loop" );
59
- for (int i =0 ;i <rndcnt ;i ++)
60
- {
61
- Random r1 =new Random ();
62
- int index =r1 .nextInt (size );
63
- c .moveToPosition (index );
64
- Log .e ("hi" ,index + "in for loop" );
65
- id [i ]=c .getInt (0 );
66
- name [i ]=c .getString (1 ); //8 for name,12 for name
67
- number [i ]=c .getString (2 );
68
- Log .e ("hi" , id +" " +name [i ]+" " +number [i ]);
69
- //insert this records into database;
70
- }
71
-
86
+ //Here find out if application run for first time or not.
87
+ //If not first time then find the last scanned date of application.
72
88
73
89
74
- final Cursor cursor = managedQuery (Phone .CONTENT_URI , projection , null , null ,Phone .DISPLAY_NAME + " ASC" );
90
+ /*SharedPreferences appSettings=getSharedPreferences("RunAppPreferences", MODE_PRIVATE);
91
+ String d=appSettings.getString("Counter",null);
92
+ SharedPreferences.Editor editor=appSettings.edit();
75
93
76
- final ListView listView1 = (ListView )findViewById (R .id .listView1 );
77
-
78
- adapter = new SimpleCursorAdapter (
79
- this , //Context
80
- R .layout .contactslist , //xml definintion of each listView item
81
- cursor , //Cursor
82
- new String [] {"display_name" ,Phone .NUMBER ,"_id" }, //Columns to select From
83
- new int [] {R .id .textView11 ,R .id .textView12 ,R .id .checkBox1 } //Object to bind to
84
- );
85
- listView1 .setAdapter (adapter );
94
+ String d1=d1=appSettings.getString("runDate", null);
95
+ if(d1==null)
96
+ {
97
+ //d=DateFormat.getDateFormat(getApplicationContext()).toString();
98
+ //Toast.makeText(this, d+"hi",Toast.LENGTH_LONG);
99
+ */
100
+ //if(tempvar==false)
101
+ if (k ==0 )
102
+ {
103
+ for (int i =0 ;i <rndcnt ;i ++)
104
+ {
105
+ Random r1 =new Random ();
106
+ int index =r1 .nextInt (size );
107
+ c .moveToPosition (index );
108
+ Log .e ("hi" ,index + "in for loop" );
109
+ id [i ]=c .getInt (0 );
110
+ name [i ]=c .getString (1 );
111
+ number [i ]=c .getString (2 );
112
+ Log .e ("hi" , id +" " +name [i ]+" " +number [i ]);
113
+ //insert this records into database;
114
+ Log .e ("hi" , "after log e" );
115
+ con .insertNumber (name [i ],number [i ], 1 );
116
+ }
117
+ c .close ();
118
+ //editor.putString("rundate",d);
119
+ //editor.commit();
120
+ }
121
+ else if (k ==1 )
122
+ {
123
+ //if App run after 10 days
124
+
125
+ for (int i =0 ;i <rndcnt ;i ++)
126
+ {
127
+ Random r1 =new Random ();
128
+ int index =r1 .nextInt (size );
129
+ c .moveToPosition (index );
130
+ Log .e ("hi" ,index + "in for loop" );
131
+ id [i ]=c .getInt (0 );
132
+ name [i ]=c .getString (1 );
133
+ number [i ]=c .getString (2 );
134
+ Log .e ("hi" , id +" " +name [i ]+" " +number [i ]);
135
+ //insert this records into database;
136
+ Log .e ("hi" , "after log e" );
137
+ con .insertNumber (name [i ],number [i ], 1 );
138
+ }
139
+ }
140
+ else if (k ==2 )
141
+ {
142
+ //if app run before 10 days then just display the active contacts(status=1)
143
+
144
+ }
145
+
146
+ Cursor c1 =con .getAllContacts ();
147
+ final ListView listView1 = (ListView )findViewById (R .id .listView1 );
148
+ adapter = new SimpleCursorAdapter (this ,R .layout .contactslist ,c1 ,new String [] {"c_name" ,"c_no" ,"_id" },new int [] {R .id .textView11 ,R .id .textView12 ,R .id .checkBox1 });
149
+ listView1 .setAdapter (adapter );
150
+ c1 .close ();
86
151
}
87
-
88
-
89
- }
152
+
153
+ private void copyDB (InputStream inputStream , OutputStream outputStream )
154
+ throws IOException {
155
+ byte [] buffer = new byte [1024 ];
156
+ int length ;
157
+ while ((length = inputStream .read (buffer )) > 0 ) {
158
+ outputStream .write (buffer , 0 , length );
159
+ }
160
+ inputStream .close ();
161
+ outputStream .close ();
162
+ }
163
+ }
0 commit comments