diff --git a/app/src/main/java/com/itds/sms/ping/MainActivity.java b/app/src/main/java/com/itds/sms/ping/MainActivity.java index bcb029d..71b5f90 100644 --- a/app/src/main/java/com/itds/sms/ping/MainActivity.java +++ b/app/src/main/java/com/itds/sms/ping/MainActivity.java @@ -6,6 +6,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; @@ -17,14 +18,21 @@ import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AppCompatActivity; import android.telephony.SmsManager; +import android.util.ArraySet; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.EditText; +import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; +import java.util.ArrayList; +import java.util.Arrays; + import static java.security.AccessController.getContext; public final class MainActivity extends AppCompatActivity { @@ -42,12 +50,22 @@ public final class MainActivity extends AppCompatActivity { MenuItem pickContact; public final static int MENU_ITEM_PICK_CONTACT = 999; + MenuItem clearHistory; + public final static int MENU_ITEM_CLEAR_HISTORY = 998; + + SharedPreferences preferences; + public final static String PREF_LAST_NUMBER = "pref_last_number"; + public final static String PREF_HISTORY = "pref_history"; + + ArrayAdapter historyAdapter; + ArrayList historyContent = new ArrayList<>(); PendingIntent sentPI; PendingIntent deliveryPI; EditText phoneNumber; TextView statusText, resultText; + ListView historyList; @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,13 +75,17 @@ protected void onCreate(Bundle savedInstanceState) { phoneNumber = (EditText) findViewById(R.id.phoneNumber); statusText = (TextView) findViewById(R.id.sendStatus); resultText = (TextView) findViewById(R.id.resultStatus); + historyList = (ListView) findViewById(R.id.historyList); + preferences = getPreferences(Context.MODE_PRIVATE); + phoneNumber.setText(preferences.getString(PREF_LAST_NUMBER, getString(R.string.phonenumber))); findViewById(R.id.sendButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (MainActivity.this.checkPermissions()) { resultText.setText(null); + updateHistory(phoneNumber.getText().toString()); SmsManager.getDefault().sendDataMessage(phoneNumber.getText().toString(), null, (short) 9200, payload, sentPI, deliveryPI); } } @@ -72,6 +94,37 @@ public void onClick(View v) { sentPI = PendingIntent.getBroadcast(this, 0x1337, new Intent(SENT), PendingIntent.FLAG_CANCEL_CURRENT); deliveryPI = PendingIntent.getBroadcast(this, 0x1337, new Intent(DELIVER), PendingIntent.FLAG_CANCEL_CURRENT); + historyAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_2, android.R.id.text1); + historyList.setAdapter(historyAdapter); + updateHistory(null); + + historyList.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + phoneNumber.setText(historyAdapter.getItem(position)); + } + }); + } + + void updateHistory(String current) { + if (current != null) { + preferences.edit().putString(PREF_LAST_NUMBER, current).apply(); + preferences.edit().putString(PREF_HISTORY, preferences.getString(PREF_HISTORY, "").concat(current + ",")).apply(); + } + + historyContent.clear(); + historyContent.addAll(Arrays.asList(preferences.getString(PREF_HISTORY, "").split(","))); + + if (historyAdapter != null) { + historyAdapter.clear(); + historyAdapter.addAll(historyContent); + historyAdapter.notifyDataSetChanged(); + } + } + + void clearHistory() { + preferences.edit().putString(PREF_HISTORY, "").apply(); + updateHistory(null); } boolean checkPermissions() { @@ -85,7 +138,7 @@ boolean checkPermissions() { return true; } - private String getLogBytesHex(byte[] array) { + String getLogBytesHex(byte[] array) { StringBuilder sb = new StringBuilder(); for (byte b : array) { sb.append(String.format("0x%02X ", b)); @@ -113,7 +166,13 @@ public boolean onCreateOptionsMenu(Menu menu) { if (pickContact == null) { pickContact = menu.add(Menu.NONE, MENU_ITEM_PICK_CONTACT, Menu.NONE, R.string.pick_contact) .setIcon(R.mipmap.ic_menu_invite); - MenuItemCompat.setShowAsAction(pickContact, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); + MenuItemCompat.setShowAsAction(pickContact, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); + } + clearHistory = menu.findItem(MENU_ITEM_CLEAR_HISTORY); + if (clearHistory == null) { + clearHistory = menu.add(Menu.NONE, MENU_ITEM_CLEAR_HISTORY, Menu.NONE, "Clear history") + .setIcon(android.R.drawable.ic_menu_close_clear_cancel); + MenuItemCompat.setShowAsAction(clearHistory, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); } return super.onCreateOptionsMenu(menu); } @@ -123,11 +182,14 @@ public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == MENU_ITEM_PICK_CONTACT) { pickContact(); return true; + } else if (item.getItemId() == MENU_ITEM_CLEAR_HISTORY) { + clearHistory(); + return true; } return super.onOptionsItemSelected(item); } - private void pickContact() { + void pickContact() { Intent pickIntent = new Intent(Intent.ACTION_PICK); pickIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); ActivityCompat.startActivityForResult(this, pickIntent, MENU_ITEM_PICK_CONTACT, null); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4623e9e..aa42c03 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -23,8 +23,28 @@ android:id="@+id/sendStatus" android:layout_width="match_parent" android:layout_height="wrap_content" /> + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index be35676..0cb6a7e 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -8,4 +8,5 @@ НЕ ПОЛУЧИЛОСЬ ОТПРАВИТЬ ЗАПРОС! Выбрать контакт Не удалось выбрать контакт + история: diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2a65a0d..cec88ec 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,6 +6,7 @@ DELIVERY FAILED: PHONE OFFLINE THE REQUEST IS SENT … FAILED TO SEND REQUEST - PICK CONTACT + Pick Phone from Contacts Failed to select contact + History: