Commit f2e7f42d314310919885fea6a63398c3cdc0f4ca
1 parent
3338979a
1 修改报名相关逻辑
Showing
10 changed files
with
194 additions
and
63 deletions
app/build.gradle
| ... | ... | @@ -28,7 +28,8 @@ android { |
| 28 | 28 | buildConfigField("String", "APP_ID", stringValue("802_jhbqccxw08")) |
| 29 | 29 | buildConfigField("String", "APP_KEY", stringValue("9c5619e9be747bb3b925dd215ca5923d86f12c09b9394f")) |
| 30 | 30 | buildConfigField("String", "APP_SCERET", stringValue("1e6380cb347255e777f72b5aa60f41daa68443a132aaea")) |
| 31 | -// BAIDU_MAP_APP_KEY: "OeotXc38bbnU7WmyKGA0VMahk4v35VG2" //测试 | |
| 31 | + buildConfigField("String", "BAIDU_MAP_APP_KEY", stringValue("2IIsTtvX2FA88yvDrPZedc2ZvMhxyDbN")) | |
| 32 | +// BAIDU_MAP_APP_KEY: "OeotXc38bbnU7WmyKGA0VMahk4v35VG2" //测试 | |
| 32 | 33 | |
| 33 | 34 | manifestPlaceholders = [ |
| 34 | 35 | |
| ... | ... | @@ -45,6 +46,8 @@ android { |
| 45 | 46 | buildConfigField("String", "APP_ID", stringValue("769_jetj7bq525")) |
| 46 | 47 | buildConfigField("String", "APP_KEY", stringValue("0d57b045ec0c3988a682d94c644e66b9b8e0d2b8ef937d")) |
| 47 | 48 | buildConfigField("String", "APP_SCERET", stringValue("275e8dc29274a1186d82f65bc607c231d02bcb3bfd8976")) |
| 49 | + buildConfigField("String", "BAIDU_MAP_APP_KEY", stringValue("Q7y9SGcZVs0tmhbNTiaz0jHm4S1xGzE2")) | |
| 50 | + | |
| 48 | 51 | manifestPlaceholders = [ |
| 49 | 52 | BAIDU_MAP_APP_KEY: "Q7y9SGcZVs0tmhbNTiaz0jHm4S1xGzE2" |
| 50 | 53 | // BAIDU_MAP_APP_KEY: "0bWeo04KKKHqTwozcqgyLZVd0oVhv3rZ" | ... | ... |
app/src/main/java/com/example/modulepedometer/MainActivity.java
| ... | ... | @@ -24,14 +24,16 @@ public class MainActivity extends AppCompatActivity { |
| 24 | 24 | findViewById(R.id.recordd).setOnClickListener(new View.OnClickListener() { |
| 25 | 25 | @Override |
| 26 | 26 | public void onClick(View v) { |
| 27 | - Intent intent = new Intent(MainActivity.this, RunLineActivity.class); | |
| 28 | - startActivity(intent); | |
| 27 | +// Intent intent = new Intent(MainActivity.this, RunLineActivity.class); | |
| 28 | +// startActivity(intent); | |
| 29 | + RunLineActivity.newInstance(MainActivity.this,"358916",null,null,null); | |
| 29 | 30 | } |
| 30 | 31 | }); |
| 31 | 32 | findViewById(R.id.tooll).setOnClickListener(new View.OnClickListener() { |
| 32 | 33 | @Override |
| 33 | 34 | public void onClick(View v) { |
| 34 | 35 | RaceListActivity.newInstance(MainActivity.this,"10513196",null,null); |
| 36 | + | |
| 35 | 37 | //跳转界面 |
| 36 | 38 | // CollRunningFinishActivity.startActivity(MainActivity.this); |
| 37 | 39 | } | ... | ... |
app/src/main/java/com/example/modulepedometer/MyApp.java
| ... | ... | @@ -13,7 +13,7 @@ public class MyApp extends Application { |
| 13 | 13 | super.onCreate(); |
| 14 | 14 | String userId = ""; |
| 15 | 15 | AppConfig.init(getApplicationContext(), BuildConfig.APP_ID, BuildConfig.APP_KEY, BuildConfig.APP_SCERET, |
| 16 | - 0, BuildConfig.DEBUG, "", "", | |
| 16 | + 0, BuildConfig.DEBUG, BuildConfig.BAIDU_MAP_APP_KEY, "", | |
| 17 | 17 | userId, "", "", "", "","","",""); |
| 18 | 18 | initARouter(); |
| 19 | 19 | //在使用SDK各组件之前初始化context信息,传入ApplicationContext | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/frame/presenter/RegistrationInfoPresenter.java
| 1 | 1 | package com.cnlive.moudle.pedometer.frame.presenter; |
| 2 | 2 | |
| 3 | +import android.app.Activity; | |
| 3 | 4 | import android.content.Context; |
| 5 | +import android.util.Log; | |
| 4 | 6 | import android.widget.Toast; |
| 5 | 7 | |
| 8 | +import com.cnlive.libs.base.application.AppConfig; | |
| 6 | 9 | import com.cnlive.libs.base.logic.Logic; |
| 7 | 10 | import com.cnlive.libs.base.logic.callback.DataCallback; |
| 8 | 11 | import com.cnlive.libs.base.logic.callback.FailureCallback; |
| 9 | 12 | import com.cnlive.libs.base.logic.callback.SuccessCallback; |
| 10 | 13 | import com.cnlive.moudle.pedometer.frame.view.RegistrationInfoView; |
| 14 | +import com.cnlive.moudle.pedometer.model.Constant; | |
| 15 | +import com.cnlive.moudle.pedometer.net.ApiServiceBaiDu; | |
| 11 | 16 | import com.cnlive.moudle.pedometer.net.ApiServiceRunLin; |
| 12 | 17 | import com.cnlive.moudle.pedometer.net.SubscriptionRequest; |
| 18 | +import com.cnlive.moudle.pedometer.net.bean.BaiduTraceBean; | |
| 13 | 19 | import com.cnlive.moudle.pedometer.net.bean.BaseInfo; |
| 20 | +import com.cnlive.moudle.pedometer.utils.Sha1Util; | |
| 21 | +import com.example.moudle_pedometer.BuildConfig; | |
| 14 | 22 | import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter; |
| 15 | 23 | |
| 16 | 24 | import java.util.HashMap; |
| ... | ... | @@ -24,20 +32,20 @@ import io.reactivex.disposables.Disposable; |
| 24 | 32 | */ |
| 25 | 33 | public class RegistrationInfoPresenter extends MvpBasePresenter<RegistrationInfoView> { |
| 26 | 34 | |
| 27 | - public void commitInfo(Context context,String eventId,String sid,String mobile,String sex,String birthday){ | |
| 35 | + public void commitInfo(Context context, String eventId, String sid, String mobile, String sex, String birthday) { | |
| 28 | 36 | HashMap<String, String> map = new HashMap<>(); |
| 29 | 37 | map.put("eventId", eventId); |
| 30 | 38 | map.put("sid", sid); |
| 31 | 39 | map.put("mobile", mobile); |
| 32 | - if(sex.equals("女")){ | |
| 40 | + if (sex.equals("女")) { | |
| 33 | 41 | sex = "1"; |
| 34 | - }else if (sex.equals("男")){ | |
| 42 | + } else if (sex.equals("男")) { | |
| 35 | 43 | sex = "0"; |
| 36 | 44 | } |
| 37 | 45 | map.put("sex", sex); |
| 38 | - if(birthday.contains("年")) birthday = birthday.replace("年","-"); | |
| 39 | - if(birthday.contains("月")) birthday = birthday.replace("月","-"); | |
| 40 | - if(birthday.contains("日")) birthday = birthday.replace("日",""); | |
| 46 | + if (birthday.contains("年")) birthday = birthday.replace("年", "-"); | |
| 47 | + if (birthday.contains("月")) birthday = birthday.replace("月", "-"); | |
| 48 | + if (birthday.contains("日")) birthday = birthday.replace("日", ""); | |
| 41 | 49 | map.put("birthday", birthday); |
| 42 | 50 | Logic.create(map).action(new Logic.Action<Map<String, String>, BaseInfo>() { |
| 43 | 51 | @Override |
| ... | ... | @@ -47,15 +55,53 @@ public class RegistrationInfoPresenter extends MvpBasePresenter<RegistrationInfo |
| 47 | 55 | }).<BaseInfo>event().setFailureCallback(new FailureCallback() { |
| 48 | 56 | @Override |
| 49 | 57 | public void onFailure(int code, String message) { |
| 50 | - Toast.makeText(context,"报名失败",Toast.LENGTH_LONG).show(); | |
| 58 | + Toast.makeText(context, "报名失败", Toast.LENGTH_LONG).show(); | |
| 51 | 59 | } |
| 52 | 60 | }).setSuccessCallback(new SuccessCallback<BaseInfo>() { |
| 53 | 61 | @Override |
| 54 | 62 | public void onSuccess(BaseInfo registerInfo) { |
| 55 | - if (getView() != null){ | |
| 63 | + if (getView() != null) { | |
| 56 | 64 | getView().toResult(); |
| 57 | 65 | } |
| 58 | 66 | } |
| 59 | 67 | }).start(); |
| 60 | 68 | } |
| 69 | + | |
| 70 | + public void addEntity(Activity context, String entityName, CallBack callBack) { | |
| 71 | + HashMap<String, String> map = new HashMap<>(); | |
| 72 | + map.put("mcode", Sha1Util.getSHA1(context) + ";" + context.getPackageName()); | |
| 73 | + map.put("ak", AppConfig.getBaiduAppKey()); | |
| 74 | + map.put("service_id", Constant.SERVICE_ID + ""); | |
| 75 | + map.put("entity_name", entityName); | |
| 76 | + | |
| 77 | + Logic.create(map).action(new Logic.Action<Map<String, String>, BaiduTraceBean>() { | |
| 78 | + @Override | |
| 79 | + public Disposable action(Map<String, String> stringStringMap, DataCallback<BaiduTraceBean> dataCallback) { | |
| 80 | + return SubscriptionRequest.service(ApiServiceBaiDu.class, api -> api.addEntity(stringStringMap)).subscribe(context, dataCallback); | |
| 81 | + } | |
| 82 | + }).<BaiduTraceBean>event().setFailureCallback(new FailureCallback() { | |
| 83 | + @Override | |
| 84 | + public void onFailure(int code, String message) { | |
| 85 | + if (null == getView() || null == callBack) { | |
| 86 | + return; | |
| 87 | + } | |
| 88 | + callBack.onFailed(message); | |
| 89 | + | |
| 90 | + } | |
| 91 | + }).setSuccessCallback(new SuccessCallback<BaiduTraceBean>() { | |
| 92 | + @Override | |
| 93 | + public void onSuccess(BaiduTraceBean baiduTraceBean) { | |
| 94 | + if (null == getView() || null == callBack) { | |
| 95 | + return; | |
| 96 | + } | |
| 97 | + callBack.onSuccess(baiduTraceBean.getStatus(), baiduTraceBean.getMessage()); | |
| 98 | + } | |
| 99 | + }).start(); | |
| 100 | + } | |
| 101 | + | |
| 102 | + public interface CallBack { | |
| 103 | + void onSuccess(int state, String msg); | |
| 104 | + | |
| 105 | + void onFailed(String msg); | |
| 106 | + } | |
| 61 | 107 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/frame/view/RunRaceDetailFragmentView.java
| ... | ... | @@ -1028,9 +1028,19 @@ public class RunRaceDetailFragmentView implements MvpView, SensorEventListener { |
| 1028 | 1028 | isDifferentStreet = true; |
| 1029 | 1029 | } |
| 1030 | 1030 | |
| 1031 | - } else { | |
| 1032 | - fragment.binding.tvRunNow.setText("立即开始"); | |
| 1031 | + } | |
| 1032 | + | |
| 1033 | + if (null == runRaceDetailInfo) { | |
| 1034 | + return; | |
| 1035 | + } | |
| 1036 | + if (runRaceDetailInfo.isSignResult()) { | |
| 1033 | 1037 | fragment.binding.rlScrollViewStartBtn.setTag("1"); |
| 1038 | + fragment.binding.tvRunNow.setText("现在开始"); | |
| 1039 | + fragment.binding.tvGoTip.setVisibility(View.VISIBLE); | |
| 1040 | + } else { | |
| 1041 | + fragment.binding.rlScrollViewStartBtn.setTag("0"); | |
| 1042 | + fragment.binding.tvRunNow.setText("立即报名"); | |
| 1043 | + fragment.binding.tvGoTip.setVisibility(View.GONE); | |
| 1034 | 1044 | } |
| 1035 | 1045 | //判断赛事是否已经结束 |
| 1036 | 1046 | checkTheRaceIsEnd(); |
| ... | ... | @@ -1245,6 +1255,9 @@ public class RunRaceDetailFragmentView implements MvpView, SensorEventListener { |
| 1245 | 1255 | fragment.binding.rlScrollViewStartBtn.setTag("1"); |
| 1246 | 1256 | fragment.binding.tvRunNow.setText("现在开始"); |
| 1247 | 1257 | fragment.binding.tvGoTip.setVisibility(View.VISIBLE); |
| 1258 | + if (null != runRaceDetailInfo) { | |
| 1259 | + runRaceDetailInfo.setSignResult(true); | |
| 1260 | + } | |
| 1248 | 1261 | } |
| 1249 | 1262 | |
| 1250 | 1263 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/net/ApiServiceBaiDu.java
0 → 100644
| 1 | +package com.cnlive.moudle.pedometer.net; | |
| 2 | + | |
| 3 | +import com.cnlive.moudle.pedometer.net.bean.BaiduTraceBean; | |
| 4 | +import com.cnlive.moudle.pedometer.net.bean.BaseInfo; | |
| 5 | +import com.cnlive.moudle.pedometer.net.bean.upload.StorageInfoData; | |
| 6 | +import com.cnlive.moudle.pedometer.net.bean.upload.UploadAuthData; | |
| 7 | + | |
| 8 | +import java.util.List; | |
| 9 | +import java.util.Map; | |
| 10 | + | |
| 11 | +import io.reactivex.Observable; | |
| 12 | +import retrofit2.adapter.rxjava2.Result; | |
| 13 | +import retrofit2.http.POST; | |
| 14 | +import retrofit2.http.QueryMap; | |
| 15 | + | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * 百度官方接口 | |
| 19 | + */ | |
| 20 | +public interface ApiServiceBaiDu { | |
| 21 | + | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * 上传鉴权 | |
| 25 | + * | |
| 26 | + * @param maps | |
| 27 | + * @return | |
| 28 | + */ | |
| 29 | + @POST("api/v3/entity/add") | |
| 30 | + Observable<Result<BaiduTraceBean>> addEntity(@QueryMap Map<String, String> maps); | |
| 31 | + | |
| 32 | + | |
| 33 | +} | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/net/SubscriptionRequest.java
| ... | ... | @@ -37,6 +37,8 @@ public class SubscriptionRequest<Observable extends io.reactivex.Observable<Resu |
| 37 | 37 | public static final String SP_DEBUG_URL = "http://apiwjjtest.cnlive.com/"; |
| 38 | 38 | //OPEN API 接口域名 |
| 39 | 39 | private Observable observable; |
| 40 | + //百度官方接口 | |
| 41 | + public static final String BAIDU_TRACE_URL = "http://yingyan.baidu.com/"; | |
| 40 | 42 | |
| 41 | 43 | private static <T> String baseCMSEndpoint() { |
| 42 | 44 | return AppConfig.isDebug() ? SP_CMS_URL_DEBUT : SP_CMS_URL; |
| ... | ... | @@ -49,6 +51,8 @@ public class SubscriptionRequest<Observable extends io.reactivex.Observable<Resu |
| 49 | 51 | private static <T> String serviceEndpoint(Class<T> clazz) { |
| 50 | 52 | if (clazz == ApiServiceOpen.class) { |
| 51 | 53 | return OPEN_URL; |
| 54 | + } else if (clazz == ApiServiceBaiDu.class) { | |
| 55 | + return BAIDU_TRACE_URL; | |
| 52 | 56 | } else { |
| 53 | 57 | return baseEndpoint(); |
| 54 | 58 | } | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/net/bean/BaiduTraceBean.java
0 → 100644
| 1 | +package com.cnlive.moudle.pedometer.net.bean; | |
| 2 | + | |
| 3 | +public class BaiduTraceBean extends BaseInfo{ | |
| 4 | + private int status; | |
| 5 | + private String message; | |
| 6 | + | |
| 7 | + public int getStatus() { | |
| 8 | + return status; | |
| 9 | + } | |
| 10 | + | |
| 11 | + public void setStatus(int status) { | |
| 12 | + this.status = status; | |
| 13 | + } | |
| 14 | + | |
| 15 | + @Override | |
| 16 | + public String getMessage() { | |
| 17 | + return message; | |
| 18 | + } | |
| 19 | + | |
| 20 | + @Override | |
| 21 | + public void setMessage(String message) { | |
| 22 | + this.message = message; | |
| 23 | + } | |
| 24 | +} | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/ui/fragment/RegistrationInfoFragment.java
| ... | ... | @@ -24,6 +24,7 @@ import com.baidu.trace.model.LocationMode; |
| 24 | 24 | import com.baidu.trace.model.OnTraceListener; |
| 25 | 25 | import com.baidu.trace.model.PushMessage; |
| 26 | 26 | import com.cnlive.libs.base.frame.fragment.MvpBindingFragment; |
| 27 | +import com.cnlive.libs.base.util.AlertUtil; | |
| 27 | 28 | import com.cnlive.moudle.pedometer.commonInfo.CommonInfo; |
| 28 | 29 | import com.cnlive.moudle.pedometer.frame.presenter.RegistrationInfoPresenter; |
| 29 | 30 | import com.cnlive.moudle.pedometer.frame.view.RegistrationInfoView; |
| ... | ... | @@ -68,16 +69,17 @@ public class RegistrationInfoFragment extends MvpBindingFragment<RegistrationInf |
| 68 | 69 | * @param fenceId |
| 69 | 70 | */ |
| 70 | 71 | public void addMonitorToFence(int fenceId) { |
| 72 | + if (null == getPresenter()) { | |
| 73 | + return; | |
| 74 | + } | |
| 71 | 75 | QMUITipDialogUtil.loadingDialog(getContext(), "请稍后", false); |
| 72 | 76 | LBSTraceClient mClient = new LBSTraceClient(getContext()); |
| 73 | 77 | // 设置定位和打包周期 |
| 74 | 78 | mClient.setInterval(Constant.GATHER_INTERVAL, Constant.PACK_INTERVAL); |
| 75 | 79 | //设置定位模式 |
| 76 | 80 | mClient.setLocationMode(LocationMode.High_Accuracy); |
| 77 | - Trace mTrace = new Trace(Constant.SERVICE_ID, CommonInfo.getUserId(getContext())); | |
| 78 | 81 | //将设备添加到围栏中 |
| 79 | 82 | AddMonitoredPersonRequest addMonitoredPersonRequest = AddMonitoredPersonRequest.buildServerRequest(Constant.TAG, Constant.SERVICE_ID, fenceId, CommonInfo.getUserId(getContext())); |
| 80 | - | |
| 81 | 83 | // 初始化围栏监听器 |
| 82 | 84 | OnFenceListener onFenceListener = new OnFenceListener() { |
| 83 | 85 | // 创建围栏回调 |
| ... | ... | @@ -125,7 +127,9 @@ public class RegistrationInfoFragment extends MvpBindingFragment<RegistrationInf |
| 125 | 127 | //将设备添加到围栏中 |
| 126 | 128 | mClient.addFenceMonitoredPerson(addMonitoredPersonRequest, this); |
| 127 | 129 | } |
| 128 | - } else if (addMonitoredPersonResponse.getStatus() == 0) { | |
| 130 | + } | |
| 131 | + //成功 | |
| 132 | + else if (addMonitoredPersonResponse.getStatus() == 0) { | |
| 129 | 133 | QMUITipDialogUtil.dismessDialog(); |
| 130 | 134 | getPresenter().commitInfo(getActivity(), CommonInfo.getStreetId(getActivity()), CommonInfo.getUserId(getActivity()), "15210479774", |
| 131 | 135 | binding.sexText.getText().toString().trim().equals("未选择") ? "" : binding.sexText.getText().toString().trim(), |
| ... | ... | @@ -144,62 +148,27 @@ public class RegistrationInfoFragment extends MvpBindingFragment<RegistrationInf |
| 144 | 148 | public void onListMonitoredPersonCallback(ListMonitoredPersonResponse listMonitoredPersonResponse) { |
| 145 | 149 | } |
| 146 | 150 | }; |
| 147 | - | |
| 148 | - | |
| 149 | - //启动鹰眼服务 | |
| 150 | - OnTraceListener traceListener = new OnTraceListener() { | |
| 151 | - @Override | |
| 152 | - public void onBindServiceCallback(int i, String s) { | |
| 153 | - | |
| 154 | - } | |
| 155 | - | |
| 156 | - @Override | |
| 157 | - public void onStartTraceCallback(int i, String s) { | |
| 158 | - if (0 == i) { | |
| 159 | - //开启采集 | |
| 160 | - mClient.startGather(this); | |
| 161 | - } else { | |
| 162 | - QMUITipDialogUtil.dismessDialog(); | |
| 163 | - } | |
| 164 | - } | |
| 165 | - | |
| 166 | - @Override | |
| 167 | - public void onStopTraceCallback(int i, String s) { | |
| 168 | - | |
| 169 | - } | |
| 170 | - | |
| 151 | + //将entity添加到百度服务中 | |
| 152 | + getPresenter().addEntity(getActivity(), CommonInfo.getUserId(getActivity()), new RegistrationInfoPresenter.CallBack() { | |
| 171 | 153 | @Override |
| 172 | - public void onStartGatherCallback(int i, String s) { | |
| 173 | - if (0 == i) { | |
| 174 | - mClient.stopGather(this); | |
| 154 | + public void onSuccess(int state, String msg) { | |
| 155 | + QMUITipDialogUtil.dismessDialog(); | |
| 156 | + if (0 == state || 3005 == state) { | |
| 175 | 157 | //将设备添加到围栏中 |
| 176 | 158 | mClient.addFenceMonitoredPerson(addMonitoredPersonRequest, onFenceListener); |
| 177 | - } else { | |
| 178 | - QMUITipDialogUtil.dismessDialog(); | |
| 159 | + }else { | |
| 160 | + AlertUtil.showDeftToast(getContext(), msg); | |
| 179 | 161 | } |
| 180 | 162 | } |
| 181 | 163 | |
| 182 | 164 | @Override |
| 183 | - public void onStopGatherCallback(int i, String s) { | |
| 184 | - if (0 == i) { | |
| 185 | - mClient.stopTrace(mTrace, this); | |
| 186 | - } else { | |
| 187 | - QMUITipDialogUtil.dismessDialog(); | |
| 188 | - } | |
| 165 | + public void onFailed(String msg) { | |
| 166 | + QMUITipDialogUtil.dismessDialog(); | |
| 167 | + AlertUtil.showDeftToast(getContext(), msg); | |
| 189 | 168 | } |
| 190 | 169 | |
| 191 | - @Override | |
| 192 | - public void onPushCallback(byte b, PushMessage pushMessage) { | |
| 193 | - | |
| 194 | - } | |
| 170 | + }); | |
| 195 | 171 | |
| 196 | - @Override | |
| 197 | - public void onInitBOSCallback(int i, String s) { | |
| 198 | - | |
| 199 | - } | |
| 200 | - }; | |
| 201 | - //启动鹰眼服务 | |
| 202 | - mClient.startTrace(mTrace, traceListener); | |
| 203 | 172 | |
| 204 | 173 | } |
| 205 | 174 | ... | ... |
moudle_pedometer/src/main/java/com/cnlive/moudle/pedometer/utils/Sha1Util.java
0 → 100644
| 1 | +package com.cnlive.moudle.pedometer.utils; | |
| 2 | + | |
| 3 | +import android.content.Context; | |
| 4 | +import android.content.pm.PackageInfo; | |
| 5 | +import android.content.pm.PackageManager; | |
| 6 | + | |
| 7 | +import java.security.MessageDigest; | |
| 8 | +import java.security.NoSuchAlgorithmException; | |
| 9 | +import java.util.Locale; | |
| 10 | + | |
| 11 | +public class Sha1Util { | |
| 12 | + public static String getSHA1(Context context) { | |
| 13 | + try { | |
| 14 | + PackageInfo info = context.getPackageManager().getPackageInfo( | |
| 15 | + context.getPackageName(), PackageManager.GET_SIGNATURES); | |
| 16 | + byte[] cert = info.signatures[0].toByteArray(); | |
| 17 | + MessageDigest md = MessageDigest.getInstance("SHA1"); | |
| 18 | + byte[] publicKey = md.digest(cert); | |
| 19 | + StringBuffer hexString = new StringBuffer(); | |
| 20 | + for (int i = 0; i < publicKey.length; i++) { | |
| 21 | + String appendString = Integer.toHexString(0xFF & publicKey[i]) | |
| 22 | + .toUpperCase(Locale.US); | |
| 23 | + if (appendString.length() == 1) | |
| 24 | + hexString.append("0"); | |
| 25 | + hexString.append(appendString); | |
| 26 | + hexString.append(":"); | |
| 27 | + } | |
| 28 | + String result = hexString.toString(); | |
| 29 | + return result.substring(0, result.length() - 1); | |
| 30 | + } catch (PackageManager.NameNotFoundException e) { | |
| 31 | + e.printStackTrace(); | |
| 32 | + } catch (NoSuchAlgorithmException e) { | |
| 33 | + e.printStackTrace(); | |
| 34 | + } | |
| 35 | + return null; | |
| 36 | + } | |
| 37 | +} | ... | ... |