202 lines
8.2 KiB
Java
202 lines
8.2 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.database.Cursor;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.provider.MediaStore;
|
|
import android.view.View;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.constant.Config;
|
|
import com.gh.common.util.RandomUtils;
|
|
import com.gh.common.util.TokenUtils;
|
|
import com.gh.common.util.Utils;
|
|
import com.gh.gamecenter.retrofit.JSONObjectResponse;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
import okhttp3.MediaType;
|
|
import okhttp3.RequestBody;
|
|
import okhttp3.ResponseBody;
|
|
import retrofit2.adapter.rxjava.HttpException;
|
|
import rx.Observable;
|
|
import rx.android.schedulers.AndroidSchedulers;
|
|
import rx.functions.Func1;
|
|
import rx.schedulers.Schedulers;
|
|
|
|
/**
|
|
* Created by khy on 2017/2/10.
|
|
*/
|
|
public class SelectUserIconActivity extends BaseActivity {
|
|
|
|
@BindView(R.id.skip_media_store) TextView mSkipMediaStore;
|
|
|
|
private SharedPreferences sp;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
View contentView = View.inflate(this, R.layout.activity_select_user_icon, null);
|
|
init(contentView, "选择头像");
|
|
|
|
sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
if (data != null && requestCode == 0x123) {
|
|
Uri selectedImage = data.getData();
|
|
if (selectedImage == null) {
|
|
return;
|
|
}
|
|
String[] filePathColumn = { MediaStore.Images.Media.DATA };
|
|
|
|
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
|
|
if (cursor == null) {
|
|
return;
|
|
}
|
|
cursor.moveToFirst();
|
|
|
|
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
|
|
String picturePath = cursor.getString(columnIndex);
|
|
cursor.close();
|
|
|
|
Utils.log("picturePath = " + picturePath);
|
|
// 上传头像
|
|
Intent intent = new Intent(this, CropImageActivity.class);
|
|
intent.putExtra("path", picturePath);
|
|
intent.putExtra("entrance", "(我的光环)");
|
|
startActivityForResult(intent, 0x124);
|
|
} else if (data != null && requestCode == 0x124) {
|
|
String url = data.getExtras().getString("url");
|
|
Intent intent = new Intent();
|
|
intent.putExtra("url", url);
|
|
setResult(0x125, intent);
|
|
finish();
|
|
}
|
|
}
|
|
|
|
@OnClick({R.id.user_default_icon_1, R.id.user_default_icon_2, R.id.user_default_icon_3 ,R.id.user_default_icon_4,
|
|
R.id.user_default_icon_5, R.id.user_default_icon_6, R.id.user_default_icon_7, R.id.user_default_icon_8, R.id.skip_media_store})
|
|
public void onClick(View view){
|
|
switch (view.getId()) {
|
|
case R.id.user_default_icon_1:
|
|
postUserIocn(1);
|
|
break;
|
|
case R.id.user_default_icon_2:
|
|
postUserIocn(2);
|
|
break;
|
|
case R.id.user_default_icon_3:
|
|
postUserIocn(3);
|
|
break;
|
|
case R.id.user_default_icon_4:
|
|
postUserIocn(4);
|
|
break;
|
|
case R.id.user_default_icon_5:
|
|
postUserIocn(5);
|
|
break;
|
|
case R.id.user_default_icon_6:
|
|
postUserIocn(6);
|
|
break;
|
|
case R.id.user_default_icon_7:
|
|
postUserIocn(7);
|
|
break;
|
|
case R.id.user_default_icon_8:
|
|
postUserIocn(8);
|
|
break;
|
|
case R.id.skip_media_store:
|
|
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
|
startActivityForResult(intent, 0x123);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void postUserIocn(int i) {
|
|
postDefaultIcon(true, i);
|
|
}
|
|
|
|
public void postDefaultIcon(boolean isCheck, final int i) {
|
|
TokenUtils.getToken(this, isCheck)
|
|
.flatMap(new Func1<String, Observable<ResponseBody>>() {
|
|
@Override
|
|
public Observable<ResponseBody> call(String token) {
|
|
Map<String, String> params = new HashMap<>();
|
|
params.put("icon_key" , "icon" + i);
|
|
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
|
new JSONObject(params).toString());
|
|
return RetrofitManager.getUser().postDefaultIcon(token, body);
|
|
}
|
|
})
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new JSONObjectResponse(){
|
|
@Override
|
|
public void onResponse(JSONObject response) {
|
|
super.onResponse(response);
|
|
try {
|
|
String icon = response.getString("icon");
|
|
|
|
sp.edit().putInt("default_user_icon", i).apply();
|
|
|
|
//初始化热点名称
|
|
String chars = "abcdefghijklmnopqrstuvwxyz";
|
|
int[] randomArray = RandomUtils.getRandomArray(2, 25);
|
|
|
|
String mySsid = "ghZS-";
|
|
for (int i : randomArray) {
|
|
mySsid = mySsid + chars.charAt(i);
|
|
}
|
|
mySsid = mySsid + i;
|
|
sp.edit().putString("hotspotName", mySsid).apply();
|
|
|
|
Intent intent = new Intent();
|
|
intent.putExtra("url", icon);
|
|
setResult(0x125, intent);
|
|
finish();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
@Override
|
|
public void onFailure(HttpException e) {
|
|
super.onFailure(e);
|
|
if (e != null) {
|
|
if (e.code() == 401) {
|
|
postDefaultIcon(true, i);
|
|
return;
|
|
}
|
|
|
|
if (e.code() == 403) {
|
|
try {
|
|
JSONObject response = new JSONObject(new String(e.response().errorBody().bytes()));
|
|
if ("too frequent".equals(response.getString("detail"))) {
|
|
Toast.makeText(SelectUserIconActivity.this, "修改太频繁", Toast.LENGTH_SHORT).show();
|
|
} else {
|
|
Toast.makeText(SelectUserIconActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
|
|
}
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
Toast.makeText(SelectUserIconActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
}
|