Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky committed Feb 1, 2022
0 parents commit 0c422c9
Show file tree
Hide file tree
Showing 45 changed files with 1,724 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/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions PRIVACY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Privacy Police

The app has nothing to store, but settings.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Re

Redirect outgoing calls to Signal or Telegram.

[<img
src="https://fdroid.gitlab.io/artwork/badge/get-it-on.png"
alt="Get it on F-Droid"
height="80">](https://f-droid.org/packages/me.lucky.re/)

[comment]: <> ([<img)

[comment]: <> ( src="https://play.google.com/intl/en_us/badges/images/generic/en-play-badge.png")

[comment]: <> ( alt="Get it on Google Play")

[comment]: <> ( height="80">]&#40;https://play.google.com/store/apps/details?id=me.lucky.re&#41;)

<img
src="https://raw.githubusercontent.com/x13a/Re/main/fastlane/metadata/android/en-US/images/phoneScreenshots/1.png"
width="30%"
height="30%">

Tiny app to redirect outgoing calls to Signal or Telegram if available.

You can cancel redirection by clicking on `Redirecting` popup.

## Permissions

* ACCESS_NETWORK_STATE - check internet is available
* CALL_PHONE - make a call via messenger
* READ_CONTACTS - check contact has a messenger record
* SYSTEM_ALERT_WINDOW - show redirecting popup and launch activity from background
* CALL_REDIRECTION - process outgoing call

## License
[![GNU GPLv3 Image](https://www.gnu.org/graphics/gplv3-127x51.png)](https://www.gnu.org/licenses/gpl-3.0.en.html)

This application is Free Software: You can use, study share and improve it at your will.
Specifically you can redistribute and/or modify it under the terms of the
[GNU General Public License v3](https://www.gnu.org/licenses/gpl.html) as published by the Free
Software Foundation.
2 changes: 2 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build
/release
52 changes: 52 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
}

android {
compileSdk 32

defaultConfig {
applicationId "me.lucky.re"
minSdk 29
targetSdk 32
versionCode 1
versionName "1.0.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}

buildFeatures {
viewBinding true
}
lint {
disable 'MissingTranslation'
}
}

dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'androidx.cardview:cardview:1.0.0'
}
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
24 changes: 24 additions & 0 deletions app/src/androidTest/java/me/lucky/re/ExampleInstrumentedTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.lucky.re

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

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

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("me.lucky.re", appContext.packageName)
}
}
40 changes: 40 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.lucky.re">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-feature android:name="android.hardware.telephony" android:required="true" />

<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
android:name=".Application"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Re">

<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".CallRedirectionService"
android:exported="true"
android:permission="android.permission.BIND_CALL_REDIRECTION_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallRedirectionService"/>
</intent-filter>
</service>

</application>
</manifest>
Binary file added app/src/main/ic_launcher-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions app/src/main/java/me/lucky/re/Application.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package me.lucky.re

import android.app.Application
import com.google.android.material.color.DynamicColors

@Suppress("unused")
class Application : Application() {
override fun onCreate() {
super.onCreate()
DynamicColors.applyToActivitiesIfAvailable(this)
}
}
100 changes: 100 additions & 0 deletions app/src/main/java/me/lucky/re/CallRedirectionService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package me.lucky.re

import android.database.Cursor
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.Uri
import android.provider.ContactsContract
import android.telecom.CallRedirectionService
import android.telecom.PhoneAccountHandle

class CallRedirectionService : CallRedirectionService() {
companion object {
private const val SIGNAL_MIMETYPE = "vnd.android.cursor.item/vnd.org.thoughtcrime.securesms.call"
private const val TELEGRAM_MIMETYPE = "vnd.android.cursor.item/vnd.org.telegram.messenger.android.call"
}

private lateinit var prefs: Preferences
private lateinit var dialog: DialogWindow
private var connectivityManager: ConnectivityManager? = null

override fun onCreate() {
super.onCreate()
prefs = Preferences(this)
dialog = DialogWindow(this)
connectivityManager = getSystemService(ConnectivityManager::class.java)
}

override fun onPlaceCall(
handle: Uri,
initialPhoneAccount: PhoneAccountHandle,
allowInteractiveResponse: Boolean,
) {
if (!prefs.isServiceEnabled || !hasInternet()) {
placeCallUnmodified()
return
}
val uri = getUriFromPhoneNumber(handle.schemeSpecificPart)
if (uri != null) {
dialog.show(uri)
return
}
placeCallUnmodified()
}

private fun getContactIdByPhoneNumber(phoneNumber: String): String? {
var result: String? = null
val cursor: Cursor?
try {
cursor = contentResolver.query(
Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber)
),
arrayOf(ContactsContract.PhoneLookup._ID),
null,
null,
null,
)
} catch (exc: SecurityException) { return null }
cursor?.apply {
if (moveToFirst()) {
result = getString(getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID))
}
close()
}
return result
}

private fun getUriFromPhoneNumber(phoneNumber: String): Uri? {
val contactId = getContactIdByPhoneNumber(phoneNumber) ?: return null
var result: Uri? = null
val cursor: Cursor?
try {
cursor = contentResolver.query(
ContactsContract.Data.CONTENT_URI,
arrayOf(ContactsContract.Data._ID),
"${ContactsContract.Data.CONTACT_ID} = ? AND " +
"${ContactsContract.Data.MIMETYPE} IN (?, ?)",
arrayOf(contactId, SIGNAL_MIMETYPE, TELEGRAM_MIMETYPE),
null,
)
} catch (exc: SecurityException) { return null }
cursor?.apply {
if (moveToFirst()) {
result = Uri.withAppendedPath(
ContactsContract.Data.CONTENT_URI,
Uri.encode(getString(getColumnIndexOrThrow(ContactsContract.Data._ID))),
)
}
close()
}
return result
}

private fun hasInternet(): Boolean {
return connectivityManager
?.getNetworkCapabilities(connectivityManager?.activeNetwork)
?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false
}
}
67 changes: 67 additions & 0 deletions app/src/main/java/me/lucky/re/DialogWindow.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package me.lucky.re

import android.content.Intent
import android.graphics.PixelFormat
import android.net.Uri
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import java.util.*
import kotlin.concurrent.timerTask

class DialogWindow(private val service: CallRedirectionService) {
companion object {
private const val CANCEL_DELAY = 3000L
}

private val windowManager = service
.applicationContext
.getSystemService(WindowManager::class.java)
@Suppress("InflateParams")
private val floatView = LayoutInflater
.from(service.applicationContext)
.inflate(R.layout.popup, null)
private val layoutParams: WindowManager.LayoutParams = WindowManager.LayoutParams().apply {
format = PixelFormat.TRANSLUCENT
flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
gravity = Gravity.BOTTOM
width = WindowManager.LayoutParams.WRAP_CONTENT
height = WindowManager.LayoutParams.WRAP_CONTENT
y = 384
}
private var data: Uri? = null
private var timer: Timer? = null

init {
floatView.setOnClickListener {
timer?.cancel()
service.placeCallUnmodified()
remove()
}
}

fun show(uri: Uri?) {
remove()
timer?.cancel()
timer = Timer()
timer?.schedule(timerTask {
service.cancelCall()
remove()
Intent(Intent.ACTION_VIEW).also {
it.data = data
it.flags = Intent.FLAG_ACTIVITY_NEW_TASK
service.startActivity(it)
}
data = null
}, CANCEL_DELAY)
data = uri
windowManager?.addView(floatView, layoutParams)
}

private fun remove() {
try {
windowManager?.removeView(floatView)
} catch (exc: IllegalArgumentException) {}
}
}
Loading

0 comments on commit 0c422c9

Please sign in to comment.