From f1b0b7a28a6135293df10020b767d6b009035d57 Mon Sep 17 00:00:00 2001 From: tateisu Date: Sat, 3 Jun 2017 19:31:25 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=A4=E3=83=B3=E3=82=B9=E3=83=88=E3=83=BC?= =?UTF-8?q?=E3=83=ABID=E3=81=A8=E3=83=87=E3=83=90=E3=82=A4=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=83=BC=E3=82=AF=E3=83=B3=E3=82=92=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E3=81=99=E3=82=8B=E5=85=88=E3=82=92=E3=83=90=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=82=A2=E3=83=83=E3=83=97=E5=AF=BE=E8=B1=A1=E5=A4=96=E3=81=AE?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AB=E5=A4=89=E6=9B=B4?= =?UTF-8?q?=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/dictionaries/tateisu.xml | 1 + .idea/misc.xml | 2 +- app/build.gradle | 4 +- app/src/main/AndroidManifest.xml | 1 + .../subwaytooter/ActAccountSetting.java | 4 +- .../jp/juggler/subwaytooter/AlarmService.java | 52 +++++++++++++++---- .../juggler/subwaytooter/AppDataExporter.java | 4 +- .../MyFirebaseInstanceIDService.java | 3 +- .../java/jp/juggler/subwaytooter/Pref.java | 3 +- .../jp/juggler/subwaytooter/PrefDevice.java | 16 ++++++ .../subwaytooter/table/SavedAccount.java | 5 +- app/src/main/res/xml/backup_spec.xml | 4 ++ 12 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/jp/juggler/subwaytooter/PrefDevice.java create mode 100644 app/src/main/res/xml/backup_spec.xml diff --git a/.idea/dictionaries/tateisu.xml b/.idea/dictionaries/tateisu.xml index bb8296a0..b74115e2 100644 --- a/.idea/dictionaries/tateisu.xml +++ b/.idea/dictionaries/tateisu.xml @@ -19,6 +19,7 @@ reblogged reblogs sephiroth + sharedpref simeji styler subwaytooter diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d199810..fbb68289 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index bfc3c709..85a24b29 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "jp.juggler.subwaytooter" minSdkVersion 21 targetSdkVersion 25 - versionCode 76 - versionName "0.7.6" + versionCode 77 + versionName "0.7.7" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1f5b9770..713696b4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,6 +15,7 @@ android:supportsRtl="true" android:theme="@style/AppTheme.Light" android:largeHeap="true" + android:fullBackupContent="@xml/backup_spec" > diff --git a/app/src/main/java/jp/juggler/subwaytooter/ActAccountSetting.java b/app/src/main/java/jp/juggler/subwaytooter/ActAccountSetting.java index 8f0c11dd..9b85265d 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/ActAccountSetting.java +++ b/app/src/main/java/jp/juggler/subwaytooter/ActAccountSetting.java @@ -320,8 +320,8 @@ public class ActAccountSetting extends AppCompatActivity void unregister(){ try{ - String install_id = App1.pref.getString( Pref.KEY_INSTALL_ID, null ); - + + String install_id = PrefDevice.prefDevice( ActAccountSetting.this ).getString( PrefDevice.KEY_INSTALL_ID,null); if( TextUtils.isEmpty( install_id ) ){ log.d( "performAccountRemove: missing install_id" ); return; diff --git a/app/src/main/java/jp/juggler/subwaytooter/AlarmService.java b/app/src/main/java/jp/juggler/subwaytooter/AlarmService.java index c9637dea..e233ae36 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/AlarmService.java +++ b/app/src/main/java/jp/juggler/subwaytooter/AlarmService.java @@ -17,6 +17,8 @@ import android.support.v4.content.ContextCompat; import android.support.v4.content.WakefulBroadcastReceiver; import android.text.TextUtils; +import com.google.firebase.iid.FirebaseInstanceId; + import org.hjson.JsonObject; import org.hjson.JsonValue; import org.json.JSONArray; @@ -138,10 +140,14 @@ public class AlarmService extends IntentService { // 同期処理を行って良い @Override protected void onHandleIntent( @Nullable Intent intent ){ bStreamListenerTest =false; + // クラッシュレポートによると App1.onCreate より前にここを通る場合がある // データベースへアクセスできるようにする App1.prepareDB( this.getApplicationContext() ); + // インストールIDを生成する + // インストールID生成時にSavedAccountテーブルを操作することがあるので + // アカウントリストの取得より先に行う install_id = getInstallId(); if( intent != null ){ @@ -314,12 +320,33 @@ public class AlarmService extends IntentService { } String getInstallId(){ - String sv = pref.getString(Pref.KEY_INSTALL_ID,null); + SharedPreferences prefDevice = PrefDevice.prefDevice( this ); + + + String sv = prefDevice.getString(PrefDevice.KEY_INSTALL_ID,null); if( ! TextUtils.isEmpty( sv ) ) return sv; + // インストールIDを生成する前に、各データの通知登録キャッシュをクリアする + SavedAccount.clearRegistrationCache(); + try{ - String device_token = pref.getString( Pref.KEY_DEVICE_TOKEN, null ); - if( TextUtils.isEmpty( device_token ) ) return null; + String device_token = prefDevice.getString( PrefDevice.KEY_DEVICE_TOKEN, null ); + if( TextUtils.isEmpty( device_token ) ){ + try{ + // トークンがまだ生成されていない場合、このメソッドは null を返します。 + device_token = FirebaseInstanceId.getInstance().getToken(); + if( TextUtils.isEmpty( device_token ) ){ + log.e("getInstallId: missing device token."); + return null; + }else{ + prefDevice.edit().putString( PrefDevice.KEY_DEVICE_TOKEN, device_token ).apply(); + } + }catch(Throwable ex2){ + log.e("getInstallId: could not get device token."); + ex2.printStackTrace(); + return null; + } + } Request request = new Request.Builder() .url( APP_SERVER + "/counter" ) @@ -336,7 +363,7 @@ public class AlarmService extends IntentService { //noinspection ConstantConditions sv = Utils.digestSHA256( device_token + UUID.randomUUID() + response.body().string() ); - pref.edit().putString(Pref.KEY_INSTALL_ID, sv).apply(); + prefDevice.edit().putString(PrefDevice.KEY_INSTALL_ID, sv).apply(); return sv; @@ -346,11 +373,11 @@ public class AlarmService extends IntentService { } } - static final String REGISTER_KEY_UNREGISTERED = "unregistered"; + private void unregisterDeviceToken( @NonNull SavedAccount account ){ try{ - if( REGISTER_KEY_UNREGISTERED.equals( account.register_key ) ){ + if( SavedAccount.REGISTER_KEY_UNREGISTERED.equals( account.register_key ) ){ log.d("unregisterDeviceToken: already unregistered."); return; } @@ -383,7 +410,7 @@ public class AlarmService extends IntentService { log.e( "unregisterDeviceToken: %s", response ); if( response.isSuccessful() ){ - account.register_key = REGISTER_KEY_UNREGISTERED; + account.register_key = SavedAccount.REGISTER_KEY_UNREGISTERED; account.register_time = 0L; account.saveRegisterKey(); } @@ -402,7 +429,9 @@ public class AlarmService extends IntentService { return false; } - String device_token = pref.getString( Pref.KEY_DEVICE_TOKEN, null ); + SharedPreferences prefDevice = PrefDevice.prefDevice( this ); + + String device_token = prefDevice.getString( PrefDevice.KEY_DEVICE_TOKEN, null ); if( TextUtils.isEmpty( device_token ) ){ log.d("registerDeviceToken: missing device_token"); return false; @@ -413,8 +442,13 @@ public class AlarmService extends IntentService { log.d("registerDeviceToken: missing access_token"); return false; } - + String tag = account.notification_tag; + + if( SavedAccount.REGISTER_KEY_UNREGISTERED.equals( account.register_key ) ){ + tag = null; + } + if( TextUtils.isEmpty( tag ) ){ tag = account.notification_tag = Utils.digestSHA256( install_id + account.db_id + account.acct ); account.saveNotificationTag(); diff --git a/app/src/main/java/jp/juggler/subwaytooter/AppDataExporter.java b/app/src/main/java/jp/juggler/subwaytooter/AppDataExporter.java index 588e29bd..700f1c13 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/AppDataExporter.java +++ b/app/src/main/java/jp/juggler/subwaytooter/AppDataExporter.java @@ -338,8 +338,8 @@ public class AppDataExporter { break; // just ignore - case Pref.KEY_DEVICE_TOKEN: - case Pref.KEY_INSTALL_ID: + case "device_token": + case "install_id": reader.skipValue(); e.remove( k ); break; diff --git a/app/src/main/java/jp/juggler/subwaytooter/MyFirebaseInstanceIDService.java b/app/src/main/java/jp/juggler/subwaytooter/MyFirebaseInstanceIDService.java index ec17c6a5..a834861a 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/MyFirebaseInstanceIDService.java +++ b/app/src/main/java/jp/juggler/subwaytooter/MyFirebaseInstanceIDService.java @@ -18,8 +18,7 @@ public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { String token = FirebaseInstanceId.getInstance().getToken(); log.d("onTokenRefresh: instance_token=%s",token); - Pref.pref(this).edit().putString(Pref.KEY_DEVICE_TOKEN,token).apply(); - + PrefDevice.prefDevice(this).edit().putString(PrefDevice.KEY_DEVICE_TOKEN,token).apply(); Intent intent = new Intent(this,AlarmService.class); intent.setAction( AlarmService.ACTION_DEVICE_TOKEN ); diff --git a/app/src/main/java/jp/juggler/subwaytooter/Pref.java b/app/src/main/java/jp/juggler/subwaytooter/Pref.java index 497dab61..37ec76f8 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/Pref.java +++ b/app/src/main/java/jp/juggler/subwaytooter/Pref.java @@ -48,8 +48,7 @@ public class Pref { static final String KEY_MEDIA_THUMB_HEIGHT = "MediaThumbHeight"; static final String KEY_TIMELINE_FONT = "timeline_font"; static final String KEY_DONT_CROP_MEDIA_THUMBNAIL = "DontCropMediaThumb"; - static final String KEY_DEVICE_TOKEN = "device_token"; - static final String KEY_INSTALL_ID = "install_id"; + static final String KEY_STREAM_LISTENER_SECRET = "stream_listener_secret"; static final String KEY_STREAM_LISTENER_CONFIG_URL = "stream_listener_config_url"; diff --git a/app/src/main/java/jp/juggler/subwaytooter/PrefDevice.java b/app/src/main/java/jp/juggler/subwaytooter/PrefDevice.java new file mode 100644 index 00000000..6ae53969 --- /dev/null +++ b/app/src/main/java/jp/juggler/subwaytooter/PrefDevice.java @@ -0,0 +1,16 @@ +package jp.juggler.subwaytooter; + +import android.content.Context; +import android.content.SharedPreferences; + +public class PrefDevice { + + private static String file_name = "device"; + + static SharedPreferences prefDevice( Context context ){ + return context.getSharedPreferences( file_name, Context.MODE_PRIVATE ); + } + + static final String KEY_DEVICE_TOKEN = "device_token"; + static final String KEY_INSTALL_ID = "install_id"; +} diff --git a/app/src/main/java/jp/juggler/subwaytooter/table/SavedAccount.java b/app/src/main/java/jp/juggler/subwaytooter/table/SavedAccount.java index e5590a99..6e88af59 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/table/SavedAccount.java +++ b/app/src/main/java/jp/juggler/subwaytooter/table/SavedAccount.java @@ -311,9 +311,12 @@ public class SavedAccount extends TootAccount implements LinkClickContext { App1.getDB().update( table, cv, COL_ID + "=?", new String[]{ Long.toString( db_id ) } ); } - + + public static final String REGISTER_KEY_UNREGISTERED = "unregistered"; + public static void clearRegistrationCache(){ ContentValues cv = new ContentValues(); + cv.put( COL_REGISTER_KEY, REGISTER_KEY_UNREGISTERED ); cv.put( COL_REGISTER_TIME, 0L ); App1.getDB().update( table, cv, null, null); } diff --git a/app/src/main/res/xml/backup_spec.xml b/app/src/main/res/xml/backup_spec.xml new file mode 100644 index 00000000..1b3de7a5 --- /dev/null +++ b/app/src/main/res/xml/backup_spec.xml @@ -0,0 +1,4 @@ + + + +