【質問です】こんなの表示されて消えないのですがこのスマホはコインマイニングに利用されているのでしょうか?

通知バーから、こんなダイアログがこんなダイアログが表示されてるのですが、英語が分かりません。

Google翻訳で翻訳してみます。

LineaGenuine

This device is uncertified and it didn't pass the validation.
In order to avoid malicious usage of the device's resources, LOSCoins will be mined on this device.

LineaGenuine

このデバイスは認証されておらず、検証に合格しませんでした。
デバイスのリソースの悪意のある使用を避けるため、LOSCoinはこのデバイスでマイニングされます。

先日、Googleが Uncertified な端末は排除するとかアナウンスしてましたよね。

端末の登録

...

...

え!

誰が勝手にマイニングしとんねんな!

「No」や「Cancel」を押してみると

え!

怖くなってググります。

LineaGenuine (both 15.1 and 14.1)
Our new tool allows you to feel more comfortable knowing whether you’re running a certified build. In case the device is caught running an uncertified OS, this tool will warn the user and some measures will be taken in order to guarantee you the device’s resources aren’t being misused.
If you want to disable the persistent notification, run this command from a root shell (such as adb root / recovery or terminal app) setprop persist.lineage.nofool true and reboot the device

Changelog 17 - Building Blockchains, Genuine Guarantee, and Lucky Lock – LineageOS – LineageOS Android Distribution

どうやら、エイプリルフールネタのようです。

setprop persist.lineage.nofool true && stop && start

LOSCoins on my device? Yes/No : LineageOS

ネタで良かったです。

ちなみに、Googleに認証されている端末かどうかを確認するには、「PlayStore」アプリから、

「設定」-「端末の認証」

です。

今年もよろしくおねがいします。


FCM を Firebase コンソールから使う

以前まとめておいたつもりが時間が経つとすぐに理解できない。

FCM と Notification の併用をやめてバックグラウンド受信時にうれしがる

もう、面倒くさいので忘れていても直感的すぐに使いたいので

WEBで提供されている Firebaseコンソール(コンポーザー?)から。

 

送信時の「メッセージ」は必須である

APIから送信する場合は、「データ」のみでバックグランドでしれっと端末に渡すことができるが、

WEB画面から送信する場合は「メッセージ」の入力なしでは送信ボタンがが有効化されず送信できない。

 

送信時にアプリがフォアグラウンドの場合

通知バーは表示されない。

「データ」は、端末内で稼働している Service で受け取る。

「データ」がなければ何も起きない。

 

アプリがバックグラウンドの場合

「メッセージ」と「データ」は通知バーに入る。

「データ」のタイトルは通知バーに表示され、キーと値のペアデータは PendingIntent として通知バーに入る。

 

どう使う?

端末内でプロセスが死んでない限りはフォア/バックグラウンド共に同じ挙動としたい。

フォアグラウンド受信時にも同じ挙動をするように、サービスに処理を追加。

QucikStart のサンプル内に未稼働のコードがある。


/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

quickstart-android/MyFirebaseMessagingService.java at master · firebase/quickstart-android

結局は、起動されるActivityでIntentを受けるような処理を記述しなければならない。 これ書くと見通し悪くなる。きもい。


if (getIntent().getExtras() != null) {
    for (String key : getIntent().getExtras().keySet()) {
        Object value = getIntent().getExtras().get(key);
        Log.d(TAG, "Key: " + key + " Value: " + value);
    }
}

quickstart-android/MainActivity.java at master · firebase/quickstart-android

直感的にそのまま使うと、単に「アプリ起動を喚起するだけのもの」になってしまいがちだのだが、きもい。


Play Services 12.0.0 で android.permission.READ_PHONE_STATE が要求されている件

Google Play Console にアップすると怒られて受け付けてくれません。

"New permissions added
WARNING
Users that have the APK with version code 19 may need to accept one or more of the android.permission.READ_PHONE_STATE and android.permission.WRITE_EXTERNAL_STORAGE permissions, which may result in them not upgrading to this version of the app."

android.permission.READ_PHONE_STATE てどんな許可内容だったか。

端末のステータスとIDの読み取り
端末の電話機能へのアクセスをアプリに許可します。これにより、電話番号、端末ID、通話中かどうか、通話相手の電話番号をアプリから特定できるようになります。

危険なパーミッションです。

「通話機能の必要ないアプリでも Play Service を利用していれば弾かれる」というような状況のようです。

android.permission.READ_PHONE_STATE added with Play Services 12.0.0 [76024034] - Visible to Public - Issue Tracker

android - Why has the READ_PHONE_STATE permission been added? - Stack Overflow

以下で回避できるようです。できました。


<uses-permission
    android:name="android.permission.READ_PHONE_STATE"
    tools:node="remove" />

なんなんでしょうね。

このような混乱はきっとこの先も増えていきそうです。

ーーー
2018-04-04: 追記
修正されているようです。
Release Notes  |  Google APIs for Android  |  Google Developers


SharedPreferences で putStringSet() が1つしか保存できない。

何ですか、この動きは。

「Preference から Set を取得後、追加して再保存する」

問題なく以下のようなテストも通過します。


@Test fun getStringSet() {

  val key = "key"
  val preferences = context.getSharedPreferences(key, Context.MODE_PRIVATE)
  val beforeData = preferences.getStringSet(key, mutableSetOf())
  val beforeSize = beforeData.size

  beforeData.add(System.currentTimeMillis().toString())
  preferences.edit()
      .putStringSet(key, beforeData)
      .commit()

  val afterData = preferences.getStringSet(key, mutableSetOf())
  val afterSize = afterData.size

  println("$beforeData -> $afterData")
  println("$beforeSize -> $afterSize")
  assertEquals(beforeSize + 1, afterSize)

}


I/System.out: [1520865219872, 1520865358333] -> [1520865219872, 1520865358333]
I/System.out: 1 -> 2

しかし、何回実行してもデータが1つのままで増えていきません。


I/System.out: [1520865219872, 1520865504185] -> [1520865219872, 1520865504185]
I/System.out: 1 -> 2

I/System.out: [1520865219872, 1520865553254] -> [1520865219872, 1520865553254]
I/System.out: 1 -> 2

実際にファイルを確認してみると、


mako:/data/data/com.example.cryptocurrency/shared_prefs # cat key.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <set name="key">
        <string>1520865219872</string>
    </set>
</map>

1つしか保存されてないですね!

クソですね!

なぜなのか?

android - Misbehavior when trying to store a string set using SharedPreferences - Stack Overflow

Android compares the modified HashSet that you are trying to save using SharedPreferences.Editor.putStringSet with the current one stored on the SharedPreference, and both are the same object!!!

A possible solution is to make a copy of the Set returned by the SharedPreferences object

どうやら、同じオブジェクトの比較となるので保存してくれないようです。

読み出し後、すぐコピーすればいいのですね!


val beforeData = preferences.getStringSet(key, mutableSetOf()).toMutableSet()

なんとなく、よくある話のような気がします。


サポートライブラリのバージョンを常に一発で揃える

こんなのでました。

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.1.0, 27.0.2. Examples include com.android.support:animated-vector-drawable:27.1.0 and com.android.support:cardview-v7:27.0.2 less... (⌃F1)
There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion.)

「com.android.support:*」のバージョンは統一しておく必要があるのでしょうが、「利用しているサポートライブラリのバージョンが違う」ということなのでしょうか。

 

依存関係の確認


$ ./gradlew app:dependencies

大量の依存関係の出力から細々と見ていきます。

という感じで古いサードパーティのライブラリなどから、

「うまくバージョンを推移させながら参照できない。」

ということなのでしょう。

 

すばやく特定する

現在の最新バージョン「27.1.0」に揃えます。

行数が多くて面倒なので、grep や sort など使って推移解決できてないものを特定します。


$ ./gradlew app:dependencies | grep com.android.support: | grep -v 27.1.0 | sort | uniq
|    +--- com.android.support:customtabs:27.0.2 (*)
|    \--- com.android.support:cardview-v7:27.0.2 (*)
|    |    +--- com.android.support:cardview-v7:27.0.2
|    |    +--- com.android.support:customtabs:27.0.2

となり

「com.android.support:customtabs」
「com.android.support:cardview-v7」

の2つであることがわかります。

この2つを build.gradle にて追加明示してあげて、sync で赤線消えます。

しかし、これもだるいですね!

 

 

Gradle ResolutionStrategy を使う

この件はサポートライブラリに関してよく遭遇するので、細かく見る方法を覚えておきながら一括な定型にしておきましょう。

force で個別よりグループまとめれば簡単でしょうか。


// build.gradle (project)

// ...

allprojects {

  // ...

  // Force all of the primary support libraries to use the same version.
  configurations.all {
    resolutionStrategy {

      // force "com.android.support:support-annotations:${versions.supportLibrary}"
      // force 'com.google.code.findbugs:jsr305:3.0.0'

      eachDependency { details ->
        if (details.requested.group == 'com.android.support') {
          details.useVersion versions.supportLibrary
        }
      }
    }
  }
}

↓ 基本的なバージョン記述は前回より

あなたの build.gradle バージョン記述、きもいです。

ResolutionStrategy - Gradle DSL Version 4.6

All com.android.support libraries must use the exact same version specification - Stack Overflow