Skip to content

Commit

Permalink
Auth: Add SMS Retriever API implementation
Browse files Browse the repository at this point in the history
Does not yet have any support for the autofill / browser API parts
  • Loading branch information
mar-v-in committed Aug 22, 2023
1 parent 4a66676 commit 03836ce
Show file tree
Hide file tree
Showing 14 changed files with 876 additions and 1 deletion.
44 changes: 44 additions & 0 deletions play-services-auth-api-phone/core/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* SPDX-FileCopyrightText: 2023 microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

dependencies {
api project(':play-services-auth-api-phone')
implementation project(':play-services-base-core')

implementation "androidx.appcompat:appcompat:$appcompatVersion"
}

android {
namespace "org.microg.gms.auth.phone"

compileSdkVersion androidCompileSdk
buildToolsVersion "$androidBuildVersionTools"

defaultConfig {
versionName version
minSdkVersion androidMinSdk
targetSdkVersion androidTargetSdk
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}

compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}

kotlinOptions {
jvmTarget = 1.8
}

lintOptions {
disable 'MissingTranslation'
}
}
35 changes: 35 additions & 0 deletions play-services-auth-api-phone/core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ SPDX-FileCopyrightText: 2023 microG Project Team
~ SPDX-License-Identifier: Apache-2.0
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

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

<application>

<activity
android:name="org.microg.gms.auth.phone.UserConsentPromptActivity"
android:exported="true"
android:process=":ui"
android:theme="@style/Theme.AppCompat.DayNight.Dialog.Alert.NoActionBar" />

<activity
android:name="org.microg.gms.auth.phone.AskPermissionActivity"
android:exported="false"
android:process=":ui"
android:theme="@style/Theme.AppCompat.DayNight.Dialog.Alert.NoActionBar" />

<service
android:name="org.microg.gms.auth.phone.SmsRetrieverService"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.service.SmsRetrieverApiService.START" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* SPDX-FileCopyrightText: 2023 microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/

package org.microg.gms.auth.phone

import android.Manifest
import android.content.Intent
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.os.Message
import android.os.Messenger
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.os.bundleOf

private const val TAG = "AskPermission"
private const val REQUEST_CODE_PERMISSION = 101
private val ALLOWED_PERMISSIONS = setOf(Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_CONTACTS)

class AskPermissionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val permissions = intent.getStringArrayExtra(EXTRA_PERMISSIONS) ?: arrayOf(Manifest.permission.RECEIVE_SMS)
Log.d(TAG, "Requesting permissions: ${permissions.toList()}")
if (SDK_INT < 23 || permissions.any { it !in ALLOWED_PERMISSIONS }) {
sendReply(RESULT_CANCELED)
finish()
} else {
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE_PERMISSION)
}
}

private fun sendReply(code: Int = RESULT_OK, extras: Bundle = Bundle.EMPTY) {
intent.getParcelableExtra<Messenger>(EXTRA_MESSENGER)?.let {
it.send(Message.obtain().apply {
what = code
data = extras
})
}
setResult(code, Intent().apply { putExtras(extras) })
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
if (requestCode == REQUEST_CODE_PERMISSION) {
sendReply(extras = bundleOf(EXTRA_GRANT_RESULTS to grantResults))
finish()
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* SPDX-FileCopyrightText: 2023 microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/

package org.microg.gms.auth.phone

import android.content.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import android.os.Bundle
import android.util.Log

private const val TAG = "SmsContentProvider"

class SmsContentProvider: ContentProvider() {
override fun onCreate(): Boolean {
Log.d(TAG, "the smsContentProvider is created.")
return true
}

override fun call(method: String, arg: String?, extras: Bundle?): Bundle {
// val instance = context?.let { SmsRetrieverCore.getInstance(it.applicationContext) }
val result = Bundle()
// if (method == "queryMessage") {
// var token = extras?.getString("token")
// if (token == null) {
// token = ""
// }
// val message = instance?.queryMessage(extras?.getString("packageName"),
// SmsRetrieverUtil.convertStringToVerityToken(token))
// result.putString("message", message)
// } else {
// instance?.consumeMessage(extras?.getString("packageName"), extras?.getString("token"))
// }
return result
}

override fun getType(uri: Uri): String? {
return null
}

override fun insert(uri: Uri, values: ContentValues?): Uri? {
return null
}

override fun delete(url: Uri, selection: String?, selectionArgs: Array<String>?): Int {
return 0
}

override fun query(uri: Uri, projection: Array<String>?, selection: String?, selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
return null
}

override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?): Int {
return 0
}
}
Loading

1 comment on commit 03836ce

@TheLastGimbus
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the stuff that allows apps to autofill codes without full sms permission? If so, very excited for this!

Please sign in to comment.