Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
1isten committed Aug 13, 2019
0 parents commit 58db2d1
Show file tree
Hide file tree
Showing 34 changed files with 992 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea
/.idea/caches
/.idea/caches/build_file_checksums.ser
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
An Android(6.0+) WebView app that can detect the phone number of **incoming**/**outgoing**/**missed** calls in native Java functions through the `BroadcastReceiver` and then pass that number to WebView via the `JavascriptInterface` for the web's further handling.

Credit: [@ftvs](https://github.com/ftvs)'s [gist](https://gist.github.com/ftvs/e61ccb039f511eb288ee) ♥️
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
28 changes: 28 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "app.sten.wit"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
26 changes: 26 additions & 0 deletions app/src/androidTest/java/app/sten/wit/ExampleInstrumentedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app.sten.wit;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("app.sten.wit", appContext.getPackageName());
}
}
44 changes: 44 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="app.sten.wit">

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 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>-->
</application>

</manifest>
193 changes: 193 additions & 0 deletions app/src/main/java/app/sten/wit/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package app.sten.wit;

import java.util.Date;

import android.Manifest;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebResourceRequest;
import android.net.Uri;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

CallReceiver callReceiver;
WebView webView;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// We do not have this permission. Let's ask the user
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}

if (callReceiver == null) {
callReceiver = new CallReceiver();
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.PHONE_STATE");
intentFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
// dynamically register CallReceiver
registerReceiver(callReceiver, intentFilter);

webView = (WebView) findViewById(R.id.webview);

webView.addJavascriptInterface(new JsObject(), "sten");
webView.getSettings().setDefaultTextEncodingName("utf-8");
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);

webView.loadUrl(URL);

webView.setWebViewClient(new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Uri uri = request.getUrl();
if (uri == null) return false;
String url = uri.toString();

try {
if (!url.startsWith("http://") && !url.startsWith("https://")) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

return true;
}
} catch (Exception e){
return true;
}

return super.shouldOverrideUrlLoading(view, request);
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
// manually unregister dynamically registered CallReceiver
if (callReceiver != null) {
unregisterReceiver(callReceiver);
callReceiver = null;
}
}

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted!
} else {
// permission denied or has been cancelled
}
return;
}
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();

return true;
}

return super.onKeyDown(keyCode, event);
}

class CallReceiver extends PhonecallReceiver {

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

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;

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

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

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

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

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

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

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

class JsObject {

@JavascriptInterface
public void debug(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
}
}
Loading

0 comments on commit 58db2d1

Please sign in to comment.