176 lines
5.7 KiB
Java
176 lines
5.7 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.preference.PreferenceManager;
|
|
import android.provider.MediaStore;
|
|
import android.support.annotation.NonNull;
|
|
import android.text.TextUtils;
|
|
import android.view.View;
|
|
import android.widget.TextView;
|
|
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.util.RandomUtils;
|
|
import com.gh.common.util.UserIconUtils;
|
|
import com.lightgame.utils.Utils;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.OnClick;
|
|
|
|
/**
|
|
* Created by khy on 2017/2/10.
|
|
*/
|
|
public class SelectUserIconActivity extends BaseActivity {
|
|
|
|
@BindView(R.id.skip_media_store)
|
|
TextView mSkipMediaStore;
|
|
|
|
private SharedPreferences sp;
|
|
|
|
@NonNull
|
|
public static Intent getIntent(Context context) {
|
|
Intent intent = new Intent(context, SelectUserIconActivity.class);
|
|
return intent;
|
|
}
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.activity_select_user_icon;
|
|
}
|
|
|
|
@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 = CropImageActivity.getIntent(this, picturePath, "我的光环(选择头像)");
|
|
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();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
initTitle(getString(R.string.title_select_user_icon));
|
|
|
|
sp = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
}
|
|
|
|
@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:
|
|
|
|
String iconCount = sp.getString("updateIconCount", null);
|
|
if (!TextUtils.isEmpty(iconCount)) {
|
|
long l = System.currentTimeMillis();
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
|
String time = format.format(new Date(l));
|
|
|
|
JSONObject json;
|
|
try {
|
|
json = new JSONObject(iconCount);
|
|
String lastTime = json.getString("time");
|
|
if (lastTime.equals(time) && json.getInt("count") == 2) {
|
|
Utils.toast(SelectUserIconActivity.this, "每天最多只能上传2次头像");
|
|
return;
|
|
}
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
|
startActivityForResult(intent, 0x123);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void postUserIocn(int i) {
|
|
postDefaultIcon(i);
|
|
}
|
|
|
|
public void postDefaultIcon(final int i) {
|
|
String iconUrl = UserIconUtils.getUserIconUrl(i);
|
|
|
|
//初始化热点名称
|
|
String chars = "abcdefghijklmnopqrstuvwxyz";
|
|
int[] randomArray = RandomUtils.getRandomArray(2, 25);
|
|
|
|
String mySsid = "ghZS-";
|
|
for (int j : randomArray) {
|
|
mySsid = mySsid + chars.charAt(j);
|
|
}
|
|
mySsid = mySsid + i;
|
|
sp.edit().putString("hotspotName", mySsid).apply();
|
|
|
|
sp.edit().putInt("default_user_icon", i).apply();
|
|
|
|
sp.edit().putBoolean("changeDefaultIcon", true).apply();
|
|
|
|
Intent intent = new Intent();
|
|
intent.putExtra("url", iconUrl);
|
|
setResult(0x125, intent);
|
|
finish();
|
|
}
|
|
|
|
}
|