From 8fcb072abba41559b359888b50b3fe0a7c90c30b Mon Sep 17 00:00:00 2001 From: kehaoyuan Date: Mon, 20 Aug 2018 10:39:41 +0800 Subject: [PATCH] =?UTF-8?q?3.4=E6=95=B0=E6=8D=AE=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E9=9C=80=E6=B1=82(APP=E7=BB=9F=E8=AE=A1-=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=20=E6=9C=AA=E5=AE=8C=E6=88=90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/gh/common/util/DataLogUtils.java | 3 +- .../com/gh/common/util/DeviceTokenUtils.kt | 128 ++++++++++++++++++ .../java/com/gh/common/util/LogUtils.java | 53 ++++++++ .../java/com/gh/common/util/TokenUtils.java | 46 ------- .../gh/gamecenter/LibaoDetailActivity.java | 4 +- .../gh/gamecenter/SplashScreenActivity.java | 4 +- .../manager/DataCollectionManager.java | 6 +- .../message/MessageDetailFragment.java | 3 +- .../answer/detail/AnswerDetailFragment.java | 3 + .../com/gh/gamecenter/qa/entity/Questions.kt | 2 +- .../detail/QuestionsDetailFragment.java | 7 + 11 files changed, 202 insertions(+), 57 deletions(-) create mode 100644 app/src/main/java/com/gh/common/util/DeviceTokenUtils.kt delete mode 100644 app/src/main/java/com/gh/common/util/TokenUtils.java diff --git a/app/src/main/java/com/gh/common/util/DataLogUtils.java b/app/src/main/java/com/gh/common/util/DataLogUtils.java index 666dbf3a54..22f71adb07 100644 --- a/app/src/main/java/com/gh/common/util/DataLogUtils.java +++ b/app/src/main/java/com/gh/common/util/DataLogUtils.java @@ -6,6 +6,7 @@ import com.gh.gamecenter.retrofit.Response; import com.gh.gamecenter.retrofit.RetrofitManager; import com.halo.assistant.HaloApp; import com.lightgame.download.DownloadEntity; +import com.lightgame.utils.Util_System_Phone_State; import com.lightgame.utils.Utils; import org.json.JSONObject; @@ -42,7 +43,7 @@ public class DataLogUtils { String channel = HaloApp.getInstance().getChannel(); map.put("version", version); map.put("user", user); - map.put("device_id", TokenUtils.getDeviceId(context)); + map.put("device_id", Util_System_Phone_State.getDeviceId(context)); map.put("channel", channel); Map params = new HashMap<>(); diff --git a/app/src/main/java/com/gh/common/util/DeviceTokenUtils.kt b/app/src/main/java/com/gh/common/util/DeviceTokenUtils.kt new file mode 100644 index 0000000000..a9228ee6ee --- /dev/null +++ b/app/src/main/java/com/gh/common/util/DeviceTokenUtils.kt @@ -0,0 +1,128 @@ +package com.gh.common.util + +import android.content.Context +import android.os.Environment +import android.preference.PreferenceManager +import com.gh.gamecenter.BuildConfig +import com.gh.gamecenter.retrofit.RetrofitManager +import com.gh.gamecenter.retrofit.StringResponse +import com.halo.assistant.HaloApp +import com.lightgame.utils.Util_System_Phone_State +import com.lightgame.utils.Utils +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.schedulers.Schedulers +import java.io.File + + +object DeviceTokenUtils { + + const val DEVICE_ID = "deviceId" + + // 同步服务器时间 + @JvmStatic + @Synchronized + fun syncServerTime(context: Context) { + val sp = PreferenceManager.getDefaultSharedPreferences(context) + RetrofitManager.getInstance(context).api.time + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(object : StringResponse() { + override fun onResponse(response: String) { + if (response.matches("^[0-9]{10}$".toRegex())) { + try { + val editor = sp.edit() + editor.putLong("server_time", java.lang.Long.parseLong(response)) + editor.putLong("client_time", System.currentTimeMillis() / 1000) + editor.apply() + } catch (e: NumberFormatException) { + e.printStackTrace() + } + + } + } + }) + } + + @JvmStatic + fun getLaunchType(): LunchType { + val values = PreferenceManager.getDefaultSharedPreferences(HaloApp.getInstance().application).all + // 版本更新 + if (values.isNotEmpty()) { + for (value in values) { + if (value.key.contains("isNewFirstLaunchV")) { + return LunchType.UPDATE + } + } + } + // 再次重装 + if (!getDeviceId().isNullOrEmpty()) { + return LunchType.AGAIN + } + setDeviceId(Util_System_Phone_State.getDeviceId(HaloApp.getInstance().application)) // 保存deviceId + // 首次安装 + return LunchType.FIRST + } + + private fun getDeviceFileList(): List { + val sdCardDir = Environment.getExternalStorageDirectory() + val fileList: MutableList = ArrayList() + fileList.add(File(sdCardDir.path + "/gh/$DEVICE_ID")) + fileList.add(File(sdCardDir.path + "/system/$DEVICE_ID")) + fileList.add(File(sdCardDir.path + "/data/$DEVICE_ID")) + return fileList + } + + + @Synchronized + fun setDeviceId(deviceId: String) { + //将deviceId存到sp + val sp = PreferenceManager.getDefaultSharedPreferences(HaloApp.getInstance().application) + val edit = sp.edit() + edit.putString(DEVICE_ID, deviceId) + edit.apply() + Utils.log("saveDeviceId", "保存成功SP") + + //将deviceId存到SD卡 + for (file in getDeviceFileList()) { + try { + val parentFile = file.parentFile + if (!parentFile.exists()) parentFile.mkdirs() + file.writeText(deviceId) + Utils.log("saveDeviceId", "保存成功SDCard目录为:${file.path}") + } catch (e: Exception) { + Utils.log("保存u${DEVICE_ID}到SDCard异常${file.path} " + e.toString()) + e.printStackTrace() + } + } + } + + private fun getDeviceId(): String? { + val sp = PreferenceManager.getDefaultSharedPreferences(HaloApp.getInstance().application) + var deviceId = sp.getString(DEVICE_ID, null) + if (deviceId.isNullOrEmpty()) { + val fileList = getDeviceFileList() + for (file in fileList) { + if (file.exists()) { + try { + deviceId = file.readText() + Utils.log("getDeviceId", "获取成功DataFile$DEVICE_ID") + return deviceId + } catch (e: Exception) { + e.printStackTrace() + } + } + } + } + if (BuildConfig.DEBUG) { + Utils.log("getDeviceId", "获取成功SP$DEVICE_ID") + } + return deviceId + } + +} + +enum class LunchType { + FIRST, + UPDATE, + AGAIN +} diff --git a/app/src/main/java/com/gh/common/util/LogUtils.java b/app/src/main/java/com/gh/common/util/LogUtils.java index e1c8f232d8..03017b4a6e 100644 --- a/app/src/main/java/com/gh/common/util/LogUtils.java +++ b/app/src/main/java/com/gh/common/util/LogUtils.java @@ -1,5 +1,6 @@ package com.gh.common.util; +import android.app.Application; import android.content.Context; import android.provider.Settings; import android.text.TextUtils; @@ -20,6 +21,58 @@ import org.json.JSONObject; */ public class LogUtils { + public static void uploadDevice(LunchType launchType) { + JSONObject object = new JSONObject(); + Application application = HaloApp.getInstance().getApplication(); + try { + object.put("subject", "halo_device"); + object.put("launch_time", Utils.getTime(application)); + object.put("launch_type", launchType.name()); + object.put("network", DeviceUtils.getNetwork(application)); + object.put("device_model", android.os.Build.MODEL); + object.put("device_system", android.os.Build.VERSION.RELEASE); + } catch (JSONException e) { + e.printStackTrace(); + } + + upload(application, object); + } + + public static void uploadAnswerReadTime(String tracers, int readTime, String answerId, Questions questions) { + JSONObject object = new JSONObject(); + try { + object.put("subject", "answer"); + object.put("community_id", UserManager.getInstance().getCommunity().getId()); + object.put("community_name", UserManager.getInstance().getCommunity().getName()); + object.put("question_id", questions.getId()); + object.put("question_name", questions.getTitle()); + object.put("tracers", tracers); + object.put("answer_id", answerId); + object.put("read", readTime); + } catch (JSONException e) { + e.printStackTrace(); + } + + upload(HaloApp.getInstance().getApplication(), object); + } + + public static void uploadQuestionReadTime(String tracers, int readTime, Questions questions) { + JSONObject object = new JSONObject(); + try { + object.put("subject", "answer"); + object.put("community_id", UserManager.getInstance().getCommunity().getId()); + object.put("community_name", UserManager.getInstance().getCommunity().getName()); + object.put("question_id", questions.getId()); + object.put("question_name", questions.getTitle()); + object.put("tracers", tracers); + object.put("read", readTime); + } catch (JSONException e) { + e.printStackTrace(); + } + + upload(HaloApp.getInstance().getApplication(), object); + } + public static void uploadQuestions(Context context, String tracers, Questions questions) { if (context == null) return; diff --git a/app/src/main/java/com/gh/common/util/TokenUtils.java b/app/src/main/java/com/gh/common/util/TokenUtils.java deleted file mode 100644 index 9c6469fbdd..0000000000 --- a/app/src/main/java/com/gh/common/util/TokenUtils.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.gh.common.util; - -import android.content.Context; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; -import android.preference.PreferenceManager; - -import com.gh.gamecenter.retrofit.RetrofitManager; -import com.gh.gamecenter.retrofit.StringResponse; -import com.lightgame.utils.Util_System_Phone_State; - -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.schedulers.Schedulers; - -public class TokenUtils { - - // TODO VERSION:3.0 之后不存在deviceId - @Deprecated - public static synchronized String getDeviceId(Context context) { - return Util_System_Phone_State.getDeviceId(context); // 暂时用IMEI代替 - } - - // 获取服务器时间 - public static synchronized void getTime(Context context) { - final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); - RetrofitManager.getInstance(context).getApi().getTime() - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new StringResponse() { - @Override - public void onResponse(String response) { - if (response.matches("^[0-9]{10}$")) { - try { - Editor editor = sp.edit(); - editor.putLong("server_time", Long.parseLong(response)); - editor.putLong("client_time", System.currentTimeMillis() / 1000); - editor.apply(); - } catch (NumberFormatException e) { - e.printStackTrace(); - } - } - } - }); - } - -} diff --git a/app/src/main/java/com/gh/gamecenter/LibaoDetailActivity.java b/app/src/main/java/com/gh/gamecenter/LibaoDetailActivity.java index b39b17583f..0866e2230a 100644 --- a/app/src/main/java/com/gh/gamecenter/LibaoDetailActivity.java +++ b/app/src/main/java/com/gh/gamecenter/LibaoDetailActivity.java @@ -20,7 +20,7 @@ import com.gh.common.util.ApkActiveUtils; import com.gh.common.util.DetailDownloadUtils; import com.gh.common.util.EntranceUtils; import com.gh.common.util.LibaoUtils; -import com.gh.common.util.TokenUtils; +import com.gh.common.util.DeviceTokenUtils; import com.gh.common.view.DownloadProgressBar; import com.gh.common.view.VerticalItemDecoration; import com.gh.download.DownloadManager; @@ -286,7 +286,7 @@ public class LibaoDetailActivity extends BaseActivity implements LibaoDetailAdap } mAdapter.notifyItemChanged(0); - TokenUtils.getTime(LibaoDetailActivity.this); + DeviceTokenUtils.syncServerTime(LibaoDetailActivity.this); handler.sendEmptyMessageDelayed(0, 5000); } } diff --git a/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java b/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java index 8f7a137093..0a49adbe62 100644 --- a/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java +++ b/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java @@ -19,12 +19,12 @@ import android.widget.ImageView; import android.widget.TextView; import com.gh.base.BaseActivity; +import com.gh.common.util.DeviceTokenUtils; import com.gh.common.util.DialogUtils; import com.gh.common.util.PackageUtils; import com.gh.common.util.PlatformUtils; import com.gh.common.util.TagUtils; import com.gh.common.util.TimestampUtils; -import com.gh.common.util.TokenUtils; import com.gh.download.DownloadManager; import com.gh.gamecenter.manager.FilterManager; import com.gh.gamecenter.room.AppDatabase; @@ -192,7 +192,7 @@ public class SplashScreenActivity extends BaseActivity { } // 更新本地时间 - TokenUtils.getTime(this); + DeviceTokenUtils.syncServerTime(this); // // 上传数据---基于上报信息涉及用户权限 防止由于未授予权限导致的闪退 移动MainActivity上报 // DataCollectionManager.getInstance(getApplicationContext()).upload(); diff --git a/app/src/main/java/com/gh/gamecenter/manager/DataCollectionManager.java b/app/src/main/java/com/gh/gamecenter/manager/DataCollectionManager.java index 77b87bb93f..1024297518 100644 --- a/app/src/main/java/com/gh/gamecenter/manager/DataCollectionManager.java +++ b/app/src/main/java/com/gh/gamecenter/manager/DataCollectionManager.java @@ -5,13 +5,13 @@ import android.preference.PreferenceManager; import com.gh.common.util.Installation; import com.gh.common.util.PackageUtils; -import com.gh.common.util.TokenUtils; import com.gh.gamecenter.db.DataCollectionDao; import com.gh.gamecenter.db.info.DataCollectionInfo; import com.gh.gamecenter.retrofit.JSONObjectResponse; import com.gh.gamecenter.retrofit.Response; import com.gh.gamecenter.retrofit.RetrofitManager; import com.halo.assistant.HaloApp; +import com.lightgame.utils.Util_System_Phone_State; import com.lightgame.utils.Utils; import org.json.JSONArray; @@ -63,7 +63,7 @@ public class DataCollectionManager { String channel = HaloApp.getInstance().getChannel(); map.put("version", version); map.put("user", user); - map.put("device_id", TokenUtils.getDeviceId(mContext)); + map.put("device_id", Util_System_Phone_State.getDeviceId(mContext)); map.put("channel", channel); Map params = new HashMap<>(); params.put("type", type); @@ -161,7 +161,7 @@ public class DataCollectionManager { dataCollectionEntity.getData()); jsonObject.put("version", version); jsonObject.put("user", user); - jsonObject.put("device_id", TokenUtils.getDeviceId(mContext)); + jsonObject.put("device_id", Util_System_Phone_State.getDeviceId(mContext)); jsonObject.put("channel", channel); jsonArray.put(jsonObject.toString()); diff --git a/app/src/main/java/com/gh/gamecenter/message/MessageDetailFragment.java b/app/src/main/java/com/gh/gamecenter/message/MessageDetailFragment.java index 00b16f84e9..10f9d7a768 100644 --- a/app/src/main/java/com/gh/gamecenter/message/MessageDetailFragment.java +++ b/app/src/main/java/com/gh/gamecenter/message/MessageDetailFragment.java @@ -25,7 +25,6 @@ import com.gh.common.util.ImageUtils; import com.gh.common.util.LoginUtils; import com.gh.common.util.PostCommentUtils; import com.gh.common.util.TimestampUtils; -import com.gh.common.util.TokenUtils; import com.gh.gamecenter.R; import com.gh.gamecenter.adapter.MessageDetailAdapter; import com.gh.gamecenter.adapter.OnCommentCallBackListener; @@ -367,7 +366,7 @@ public class MessageDetailFragment extends NormalFragment implements OnCommentCa JSONObject cacheObject = new JSONObject(); JSONObject cacheUser = new JSONObject(); JSONObject userData = new JSONObject(); - cacheUser.put("_id", TokenUtils.getDeviceId(getContext())); + cacheUser.put("_id", mUserInfo.getId()); cacheUser.put("icon", mUserInfo.getIcon()); cacheUser.put("name", mUserInfo.getName()); userData.put("is_comment_own", true); diff --git a/app/src/main/java/com/gh/gamecenter/qa/answer/detail/AnswerDetailFragment.java b/app/src/main/java/com/gh/gamecenter/qa/answer/detail/AnswerDetailFragment.java index db2b3688e7..670ba98098 100644 --- a/app/src/main/java/com/gh/gamecenter/qa/answer/detail/AnswerDetailFragment.java +++ b/app/src/main/java/com/gh/gamecenter/qa/answer/detail/AnswerDetailFragment.java @@ -35,6 +35,7 @@ import com.gh.common.util.DataUtils; import com.gh.common.util.DisplayUtils; import com.gh.common.util.EntranceUtils; import com.gh.common.util.ImageUtils; +import com.gh.common.util.LogUtils; import com.gh.common.util.NewsUtils; import com.gh.common.util.ShareUtils; import com.gh.common.util.StringUtils; @@ -253,6 +254,8 @@ public class AnswerDetailFragment extends NormalFragment { mElapsedHelper.getElapsedTime(), UserManager.getInstance().getCommunity().getName(), mPath); + + LogUtils.uploadAnswerReadTime(mEntrance, mElapsedHelper.getElapsedTime(), mAnswerId, mDetailEntity.getQuestion()); } } diff --git a/app/src/main/java/com/gh/gamecenter/qa/entity/Questions.kt b/app/src/main/java/com/gh/gamecenter/qa/entity/Questions.kt index 1a5c971ec6..ce93c3fea6 100644 --- a/app/src/main/java/com/gh/gamecenter/qa/entity/Questions.kt +++ b/app/src/main/java/com/gh/gamecenter/qa/entity/Questions.kt @@ -12,7 +12,7 @@ class Questions() : Parcelable { @SerializedName("_id") var id: String = "" - var title: String = "" + var title: String? = "" @SerializedName("community_name") var communityName: String = "" diff --git a/app/src/main/java/com/gh/gamecenter/qa/questions/detail/QuestionsDetailFragment.java b/app/src/main/java/com/gh/gamecenter/qa/questions/detail/QuestionsDetailFragment.java index 50751cacfb..6e5ac80884 100644 --- a/app/src/main/java/com/gh/gamecenter/qa/questions/detail/QuestionsDetailFragment.java +++ b/app/src/main/java/com/gh/gamecenter/qa/questions/detail/QuestionsDetailFragment.java @@ -28,6 +28,7 @@ import com.gh.common.util.DataUtils; import com.gh.common.util.DialogUtils; import com.gh.common.util.DisplayUtils; import com.gh.common.util.EntranceUtils; +import com.gh.common.util.LogUtils; import com.gh.common.util.ShareUtils; import com.gh.common.util.StringUtils; import com.gh.common.util.UrlFilterUtils; @@ -47,6 +48,7 @@ import com.gh.gamecenter.qa.answer.edit.AnswerEditActivity; import com.gh.gamecenter.qa.answer.edit.AnswerEditFragment; import com.gh.gamecenter.qa.answer.fold.AnswerFoldActivity; import com.gh.gamecenter.qa.entity.AnswerEntity; +import com.gh.gamecenter.qa.entity.Questions; import com.gh.gamecenter.qa.entity.QuestionsDetailEntity; import com.gh.gamecenter.qa.questions.edit.QuestionEditActivity; import com.gh.gamecenter.qa.questions.invite.QuestionsInviteActivity; @@ -198,6 +200,11 @@ public class QuestionsDetailFragment extends ListFragment