Skip to content

Commit

Permalink
format some code, use github pages demo url
Browse files Browse the repository at this point in the history
  • Loading branch information
1isten committed Jun 16, 2020
1 parent 3de518f commit 9f9acce
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 57 deletions.
8 changes: 5 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- statically register CallReceiver
<!-- statically register CallReceiver -->
<!--
<receiver android:name=".CallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>-->
</receiver>
-->
</application>

</manifest>
</manifest>
42 changes: 23 additions & 19 deletions app/src/main/java/app/sten/wit/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

public class MainActivity extends AppCompatActivity {

public static final String URL = "https://sten.app/WhoIsThat/";

public static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;
public static final int MY_PERMISSIONS_REQUEST_PROCESS_OUTGOING_CALLS = 1;
public static final String URL = "http://112.74.170.24/";

CallReceiver callReceiver;
WebView webView;
Expand Down Expand Up @@ -121,6 +122,7 @@ public void onRequestPermissionsResult(int requestCode,
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted!
Log.d("###", "READ_PHONE_STATE granted!");
// check PROCESS_OUTGOING_CALLS permission only when READ_PHONE_STATE is granted
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.PROCESS_OUTGOING_CALLS)
Expand All @@ -132,8 +134,9 @@ public void onRequestPermissionsResult(int requestCode,
}
} else {
// permission denied or has been cancelled
Log.d("###", "READ_PHONE_STATE denied!");
Toast.makeText(getApplicationContext(),
"READ_PHONE_STATE permission missing!",
"missing READ_PHONE_STATE",
Toast.LENGTH_LONG).show();
}
break;
Expand All @@ -142,11 +145,12 @@ public void onRequestPermissionsResult(int requestCode,
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted!
Log.d("#", "Permissions granted!");
Log.d("###", "PROCESS_OUTGOING_CALLS granted!");
} else {
// permission denied or has been cancelled
Log.d("###", "PROCESS_OUTGOING_CALLS denied!");
Toast.makeText(getApplicationContext(),
"PROCESS_OUTGOING_CALLS permission missing!",
"missing PROCESS_OUTGOING_CALLS",
Toast.LENGTH_LONG).show();
}
break;
Expand All @@ -168,51 +172,51 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
class CallReceiver extends PhonecallReceiver {

@Override
protected void onIncomingCallStarted(Context ctx, String number, Date start) {
String msg = "start incoming call: " + number + " at " + start;
protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
String msg = "start outgoing call: " + number + " at " + start;

Log.d("###", msg);
Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
Log.d("#", msg);

webView.loadUrl("javascript:writeNumber('" + number + "')");
}

@Override
protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
String msg = "start outgoing call: " + number + " at " + start;
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) {
String msg = "end outgoing call: " + number + " at " + end;

Log.d("###", msg);
Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
Log.d("#", msg);

webView.loadUrl("javascript:writeNumber('" + number + "')");
}

@Override
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
String msg = "end incoming call: " + number + " at " + end;
protected void onIncomingCallStarted(Context ctx, String number, Date start) {
String msg = "start incoming call: " + number + " at " + start;

Log.d("###", msg);
Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
Log.d("#", msg);

webView.loadUrl("javascript:writeNumber('" + number + "')");
}

@Override
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) {
String msg = "end outgoing call: " + number + " at " + end;
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
String msg = "end incoming call: " + number + " at " + end;

Log.d("###", msg);
Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
Log.d("#", msg);

webView.loadUrl("javascript:writeNumber('" + number + "')");
}

@Override
protected void onMissedCall(Context ctx, String number, Date start) {
String msg = "missed call: " + number + " at " + start;
protected void onMissedCall(Context ctx, String number, Date missed) {
String msg = "missed call: " + number + " at " + missed;

Log.d("###", msg);
Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
Log.d("#", msg);

webView.loadUrl("javascript:writeNumber('" + number + "')");
}
Expand Down
59 changes: 27 additions & 32 deletions app/src/main/java/app/sten/wit/PhonecallReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,50 @@

public abstract class PhonecallReceiver extends BroadcastReceiver {

//The receiver will be recreated whenever android feels like it. We need a static variable to remember data between instantiations
// The receiver will be recreated whenever android feels like it.
// We need a static variable to remember data between instantiations

private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber; //because the passed incoming is only valid in ringing
private static String savedNumber; // because the passed incoming is only valid in ringing

@Override
public void onReceive(Context context, Intent intent) {

//We listen to two intents. The new outgoing call only tells us of an outgoing call. We use it to get the number.
// We listen to two intents. The new outgoing call only tells us of an outgoing call.
// We use it to get the number.
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
}
else{
} else {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
state = TelephonyManager.CALL_STATE_IDLE;
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}


onCallStateChanged(context, state, number);
}
}

//Derived classes should override these to respond to specific events of interest
protected void onIncomingCallStarted(Context ctx, String number, Date start){}
protected void onOutgoingCallStarted(Context ctx, String number, Date start){}
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end){}
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end){}
protected void onMissedCall(Context ctx, String number, Date start){}
// Derived classes should override these to respond to specific events of interest
protected abstract void onIncomingCallStarted(Context ctx, String number, Date start);
protected abstract void onOutgoingCallStarted(Context ctx, String number, Date start);
protected abstract void onIncomingCallEnded(Context ctx, String number, Date start, Date end);
protected abstract void onOutgoingCallEnded(Context ctx, String number, Date start, Date end);
protected abstract void onMissedCall(Context ctx, String number, Date missed);

//Deals with actual events
// Deals with actual events

//Incoming call- goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
//Outgoing call- goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
// Incoming call - goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
// Outgoing call - goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
public void onCallStateChanged(Context context, int state, String number) {
if(lastState == state){
//No change, debounce extras
if (lastState == state) {
// No change, debounce extras
return;
}
switch (state) {
Expand All @@ -66,23 +63,21 @@ public void onCallStateChanged(Context context, int state, String number) {
onIncomingCallStarted(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
if(lastState != TelephonyManager.CALL_STATE_RINGING){
// Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING){
//Ring but no pickup- a miss
// Went to idle - this is the end of a call. What type depends on previous state(s)
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
// Ring but no pickup - a miss
onMissedCall(context, savedNumber, callStartTime);
}
else if(isIncoming){
} else if (isIncoming) {
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
}
else{
} else{
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
}
break;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
</adaptive-icon>
2 changes: 1 addition & 1 deletion app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
</adaptive-icon>

0 comments on commit 9f9acce

Please sign in to comment.