20220318のAndroidに関する記事は5件です。

【Android】Android 13から始める revokeOwnPermissionOnKill

revokeOwnPermissionOnKill とは Android13 DP2(Tiramisu)から追加された機能で、Runtime Permissionを意図的に削除させることができます。 ちなみに、DP1ではselfRevokePermissionと呼ばれていたのでStable時には変わっている可能性は大いにありそうです。 以下抄訳↓ 1つ以上の権限を取り消すトリガー 取り消しは、非同期で行われる アプリがフォアグラウンドにいる間や、他のアプリがContentProviderにアクセスしている場合はトリガーは発火しない すぐ実行して欲しい場合は、 プロセスを終了させる Process.kt exitProcess(0) 実際に試してみる 1. target sdkをTiramisuDP2以上に設定する build.gradle android { compileSdkVersion "android-Tiramisu" defaultConfig { targetSdkVersion "Tiramisu" minSdkVersion "Tiramisu" } } 2. 使用するmoduleのManifestへ任意のpermissionを追加する AndroidManifest.xml <uses-permission android:name="android.permission.CAMERA" /> 3. permissionのrequestとrevoke処理を追加する MainActivity.kt companion object { // Permission private val PERMISSION_REQUIRED = Manifest.permission.CAMERA } private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> if (isGranted) { // granted } else { // not granted } } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) permitRequestBtn.setOnClickListener { if (ContextCompat.checkSelfPermission(context, PERMISSION_REQUIRED) != PackageManager.PERMISSION_GRANTED) { // request requestPermissionLauncher.launch(PERMISSION_REQUIRED) } } permitRevokeBtn.setOnClickListener { if (ContextCompat.checkSelfPermission(context, PERMISSION_REQUIRED) == PackageManager.PERMISSION_GRANTED) { // revoke context.revokeOwnPermissionOnKill(PERMISSION_REQUIRED) } } } 4. 試してみる ※わかりやすい例として、HOME'Sアプリ内かざして検索にてカメラを利用しているのでスクショを添付 Requestボタンでカメラの権限リクエスト Revokeボタンで権限の取り消しリクエスト アプリを一度キルして再度立ち上げる Request Revoke 最後に 単体だけでなく、複数権限を同時に取り消すことも可能でした。 また、取り消しリクエスト後もexitProcess(0)を呼ぶのは現実的ではないと思うので、ユーザーにアプリキルを促す体験など込みで考える必要がありそうです。 利用ケースとしては、特定の権限ありきで提供していた機能の削除時に呼んでおくといったところでしょうか。ユーザーと開発者の双方にとってメリットになる機能なので積極的に取り込んでいけると良さそうです。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

ローカル起動のサーバー/フロントアプリケーションをAndroidエミュレータのWebViewで開く

0. 概要 掲題の通り、ローカル起動のサーバー/フロントアプリケーションをAndroidエミュレータのWebViewで開く場合、 iOSシミュレーターで開く場合と違っていくつか設定が必要だったのでその方法を記載します。 1. http通信を可能にする Android9以降ではhttp通信がデフォルトで無効になったそうです。 Ref: https://developer.android.com/training/articles/security-config?hl=ja 取り急ぎローカル環境で動作確認がしたいだけなので、以下の設定を追記して有効にします。 本来無効にしておく方が望ましいので、あくまで開発時だけに留める方が良いと思います。 AndroidManifest.xml のapplicationタグに android:usesCleartextTraffic="true" を追記します。 <application android:label="hogehuga" android:icon="@mipmap/ic_launcher" android:usesCleartextTraffic="true"> // ココを追記! 2. エミュレータからPCのローカルホストに接続できるようにする エミュレータ内でのlocalhostはPCではなくエミュレータ自身を指します。 そのため以下のコマンドにて、エミュレータから手元のPCに通信を転送するようにします。 adb reverse tcp:3000 tcp:3000 ※ ポート番号はご自身の環境によってよしなに変更してください。 adbコマンドの設定を行っていない場合は以下の手順で設定を行ってください Android Studioを開きます Project Structureを開きます(ヘッダーのFileより開くことができます) Android SDKのpathを確認します (zshの場合)zprofileにexport PATH=$PATH:{AndroidSDKのpath}/platform-toolsを追記します 設定ファイルの読み直しを行いますsource ~/.zprofile 3. http://localhost:3000ではなく、http://10.0.2.2:3000をWebView開きます ※ポート番号はご自身の環境によってよしなに変更してください
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

リンクにマルウェアまたはフィッシングが仕込まれたか検出する方法

利用ライブラリ Safety Detect 対応端末 すべてのAndroid端末 対応OS Android 4.4 実装手順 前準備 (1) Huaweu Developerを登録します。 (2) AppGallery Connectでアプリのプロジェクトを登録します。 (3) keytoolで生成したSHA256をAppGallery Connectのアプリプロジェクトに登録します。 (4) agconnect-services.jsonをダウンロードし、appフォルダに配置します。 (5) Safety Detectを有効にします。 HMS SDKを導入 (1) プロジェクトのbuild.gradleを開き、mavenとclasspathを追加します。 build.gradle // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = "1.3.72" repositories { google() jcenter() // こちらの行を追加 maven {url 'http://developer.huawei.com/repo/'} } dependencies { classpath "com.android.tools.build:gradle:4.0.1" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files // こちらの行を追加 classpath 'com.huawei.agconnect:agcp:1.3.1.300' } } allprojects { repositories { google() jcenter() // こちらの行を追加 maven { url 'http://developer.huawei.com/repo/' } } } (2) モジュールのbuild.gradleを開き、次のようにを追加します。 build.gradle plugins { id 'com.android.application' id 'kotlin-android' // こちらの行を追加 id 'com.huawei.agconnect' } android { // signingConfigsを追加 signingConfigs { debug { storePassword 'My password' keyAlias 'My keyAlias' keyPassword 'My password' storeFile file('My keystore file.jks') v1SigningEnabled true v2SigningEnabled true } release { storePassword 'My password' keyAlias 'My keyAlias' keyPassword 'My password' storeFile file('My keystore file.jks') v1SigningEnabled true v2SigningEnabled true } } buildTypes { release { // signingConfigを追加 signingConfig signingConfigs.release minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } debug { // signingConfigを追加 signingConfig signingConfigs.debug debuggable true } } buildFeatures { dataBinding true } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.6.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.0' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' // こちらの1行を追加 implementation 'com.huawei.hms:safetydetect:6.3.0.301' } (3) proguard-rules.proを開き、次のようにを追加します。 proguard-rules.pro -ignorewarnings -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.huawei.hianalytics.**{*;} -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;} ライブラリの使用 SafetyDetect.getClient(activity) .initUrlCheck() .urlCheck( {Your url}, AGConnectServicesConfig.fromContext(requireContext()).getString("client/app_id"), UrlCheckThreat.MALWARE, UrlCheckThreat.PHISHING ).addOnSuccessListener { urlCheckResponse -> if (urlCheckResponse.urlCheckResponse.isEmpty()) { // 問題なし } else { urlCheckResponse.urlCheckResponse.forEach { urlCheckThreat -> when (urlCheckThreat.urlCheckResult) { UrlCheckThreat.MALWARE -> { // マルウェア } UrlCheckThreat.PHISHING -> { // フィッシング } } } } }.addOnFailureListener { exception -> exception?.printStackTrace() } GitHub https://github.com/Rei2020GitHub/MyPublicProject/tree/master/SafetyDetectDemo 参考 HMS:https://developer.huawei.com/consumer/jp/ Safety Detectの紹介:https://developer.huawei.com/consumer/jp/hms/huawei-safetydetectkit/ Safety Detectのドキュメント:https://developer.huawei.com/consumer/en/doc/development/Security-Guides/appscheck-0000001050154380 Huawei Developers:https://forums.developer.huawei.com/forumPortal/en/home Facebook Huawei Developersグループ:https://www.facebook.com/Huaweidevs/
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

JetpackComposeでDpからPxを簡単に計算する拡張関数

毎回LocalDensityを使用して取得するのは面倒なので拡張関数にする @Composable fun Hoge(){ val size = 10.dp val sizePx = with(LocalDensity.current) { size.toPx() } } 以下のような拡張関数を作成する @Composable fun Dp.toPx(): Float = with(LocalDensity.current) { this@toPx.toPx() } 使い方 @Composable fun Hoge(){ val size = 10.dp     val sizePx = size.toPx() }
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Android】DrawableからBitmapに変換

png → bitmap fun getBitmap(@DrawableRes drawableRes: Int) = BitmapFactory.decodeResource(context.resources, drawableRes) xml → bitmap fun getBitmap(@DrawableRes drawableRes: Int): Bitmap? { val drawable = getDrawable(context, drawableRes) ?: return null val canvas = Canvas() val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888) canvas.setBitmap(bitmap) drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight) drawable.draw(canvas) return bitmap } 参考サイト
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む