Skip to content
This repository has been archived by the owner on Mar 7, 2020. It is now read-only.

Commit

Permalink
SharedPrefs to contain last pinged number and history of numbers, wit…
Browse files Browse the repository at this point in the history
…h option to clear the history
  • Loading branch information
smarek committed Aug 13, 2017
1 parent 63ffe1f commit 3ffe94a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
68 changes: 65 additions & 3 deletions app/src/main/java/com/itds/sms/ping/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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<String> historyAdapter;
ArrayList<String> historyContent = new ArrayList<>();

PendingIntent sentPI;
PendingIntent deliveryPI;

EditText phoneNumber;
TextView statusText, resultText;
ListView historyList;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -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);
}
}
Expand All @@ -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() {
Expand All @@ -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));
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,28 @@
android:id="@+id/sendStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/resultStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="@string/title_history"
android:textSize="20sp" />

<ListView
android:id="@+id/historyList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="notsent">НЕ ПОЛУЧИЛОСЬ ОТПРАВИТЬ ЗАПРОС!</string>
<string name="pick_contact">Выбрать контакт</string>
<string name="pick_contact_failed">Не удалось выбрать контакт</string>
<string name="title_history">история:</string>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="offline">DELIVERY FAILED: PHONE OFFLINE</string>
<string name="sent">THE REQUEST IS SENT …</string>
<string name="notsent">FAILED TO SEND REQUEST</string>
<string name="pick_contact">PICK CONTACT</string>
<string name="pick_contact">Pick Phone from Contacts</string>
<string name="pick_contact_failed">Failed to select contact</string>
<string name="title_history">History:</string>
</resources>

0 comments on commit 3ffe94a

Please sign in to comment.