1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
package app.android.yo.yo10;
import android.Manifest;
import android.app.ActionBar;
import android.app.SearchManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.provider.ContactsContract;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
public class yo1dot0MainActivity extends AppCompatActivity {
private String namesArray[];
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
private static yo1dot0MainActivity ins;
private TTSManager ttsManager = null;
MySimpleArrayAdapter adapter;
SearchView inputSearch;
private ListView phoneContactsList;
public void dumpContact() {
phoneContactsList = (ListView) findViewById(R.id.phoneContactsList);
final String phoneNumbersArray [];
String namesCsv = "";
String numbersCsv = "";
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (name != null) {
namesCsv += name + ",";
numbersCsv += phoneNumber + ",";
}
}
phones.close();
namesArray = namesCsv.split(",");
phoneNumbersArray = numbersCsv.split(",");
adapter = new MySimpleArrayAdapter (this,namesArray);
/*adapter.sort(new Comparator<String>() {
@Override
public int compare(String arg1, String arg0) {
return arg1.compareTo(arg0);
}
});*/
phoneContactsList.setAdapter(adapter);
phoneContactsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String phoneNumberMsg = phoneNumbersArray[position];
Toast.makeText(getApplicationContext(), "Yo " + phoneNumberMsg, Toast.LENGTH_LONG).show();
sendYOasSms(phoneNumberMsg);
}
});
}
public void sendYOasSms(String number) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, "yo", null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
dumpContact();
}
else {
// Permission Denied
Toast.makeText(this, "WRITE_CONTACTS Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yo1dot0_main);
ins = this;
/*int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.READ_CONTACTS);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[] {Manifest.permission.READ_CONTACTS},REQUEST_CODE_ASK_PERMISSIONS);
return;
}*/
ttsManager = new TTSManager();
ttsManager.init(this);
//Put contacts in the list (phoneContactsList)
dumpContact();
phoneContactsList.setTextFilterEnabled(true);
/***searching a name in the listview************************/
/* phoneContactsList.setTextFilterEnabled(true);
inputSearch = (SearchView) this.findViewById(R.id.searchText);
inputSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText == null || newText.equals("")) {
phoneContactsList.clearTextFilter();
} else {
phoneContactsList.setFilterText(newText);
}
return false;
}
});*/
/***end searching a name in the listview************************/
/**Add the YO logo to the action bar**/
android.support.v7.app.ActionBar actionBar =getSupportActionBar();
actionBar.setLogo(R.mipmap.ic_yo_app);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_action,menu);
//la recherche :
SearchView sv = (SearchView) menu.findItem(R.id.searchBtn).getActionView();
sv.setBackgroundColor(Color.rgb(255, 150, 80));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText == null || newText.equals("")) {
phoneContactsList.clearTextFilter();
} else {
phoneContactsList.setFilterText(newText);
}
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.updateBtn:
dumpContact();
return true;
}
return super.onOptionsItemSelected(item);
}
public static yo1dot0MainActivity getInstance() {
return ins;
}
public void onDestroy() {
super.onDestroy();
ttsManager.shutDown();
}
public void updateTheTextView(final String msg) {
yo1dot0MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
ttsManager.initQueue(msg);
//Toast.makeText(yo1dot0MainActivity.this,msg,Toast.LENGTH_LONG).show();
/* text1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
t1.speak(msg, TextToSpeech.QUEUE_FLUSH, null);
Toast.makeText(yo1dot0MainActivity.this,msg,Toast.LENGTH_LONG).show();
onDestroy();
}
});*/
}
});
}
} |
Partager