package com.mugua.mgvideo.ui;
|
|
import android.Manifest;
|
import android.annotation.TargetApi;
|
import android.content.ContentResolver;
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.content.SharedPreferences.Editor;
|
import android.content.pm.ActivityInfo;
|
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.database.Cursor;
|
import android.net.Uri;
|
import android.os.Build;
|
import android.os.Bundle;
|
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
import android.provider.Settings;
|
import android.support.v4.app.ActivityCompat;
|
import android.telephony.TelephonyManager;
|
import android.text.TextUtils;
|
import android.util.Base64;
|
import android.view.KeyEvent;
|
import android.view.ViewGroup;
|
import android.view.animation.AlphaAnimation;
|
import android.view.animation.Animation;
|
import android.widget.ImageView;
|
import android.widget.Toast;
|
|
import com.google.gson.FieldNamingPolicy;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.lcjian.library.DeviceUuidFactory;
|
import com.lcjian.library.util.ManifestDataUtil;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.mugua.mgvideo.R;
|
import com.mugua.mgvideo.ui.dialog.UserProtocolDialog;
|
import com.mugua.mgvideo.util.UserUtil;
|
import com.umeng.analytics.MobclickAgent;
|
import com.mugua.mgvideo.BasicTextHttpResponseHandler;
|
import com.mugua.mgvideo.MGVideoAPI;
|
import com.mugua.mgvideo.MGVideoApplication;
|
import com.mugua.mgvideo.entity.Contacts;
|
import com.mugua.mgvideo.ui.main.MainActivity;
|
import com.mugua.mgvideo.util.BeibeiConstant;
|
import com.mugua.mgvideo.util.SDCardUtil;
|
import com.mugua.mgvideo.util.ad.AdTypeEnum;
|
import com.mugua.mgvideo.util.ad.CSJConstant;
|
import com.mugua.mgvideo.util.ad.SplashAdUtil;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
public class SplashActivity extends BaseActivity {
|
|
boolean isInput = false;
|
|
private ViewGroup fl_ad;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
|
if (!this.isTaskRoot()) {// 判断本activity是不是任务空间的源activity,false就是不是系统重新实例化出来的
|
finish();
|
return;
|
}
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
|
String splashLayout = ManifestDataUtil.getAppMetaData(this, "SPLASH_LAYOUT");
|
|
int resourceId = getResources().getIdentifier(splashLayout,
|
"layout", getPackageName());
|
|
|
setContentView(resourceId);
|
fl_ad = findViewById(R.id.fl_splash_ad);
|
SharedPreferences preferences = getSharedPreferences("link_page",
|
Context.MODE_PRIVATE);
|
isInput = preferences.getBoolean("isInput", false);
|
try {
|
TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
|
Build bd = new Build();
|
String model = bd.MODEL;
|
MGVideoApplication.deviceName = model;
|
if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)) {
|
MGVideoApplication.deviceNumber = manager.getDeviceId();
|
}
|
} catch (Exception e) {
|
}
|
// 初始化大设备
|
SDCardUtil.initStorage(this);
|
showProtocol();
|
}
|
|
|
private void showProtocol() {
|
String appName = getString(R.string.app_name);
|
|
String data = "感谢您对" + appName + "一直以来的信任!<br>";
|
data += "我们依照新的监管要求条例更新了";
|
data += String.format("<a href='%s'>《隐私政策》</a>和", getString(R.string.privacy));
|
data += String.format("<a href='%s'>《用户协议》</a>", getString(R.string.agreement));
|
data += "为采取相应安全保护措施,尽力保护您的个人信息安全可控,特向你说明如下:<br>" +
|
"1、您在使用" + appName + "各项产品或服务时,将会提供与具体功能相关的个人信息(可能涉及帐号、位置、存储等信息)。<br>" +
|
"2、您可以随时查询、更正、删除您的个人信息,我们也提供账户注销的渠道。<br>" +
|
"3、未经您的再次同意,我们不会将上述信息用于您未授权的其他用途或目的。<br>" +
|
"4、未经监护人同意,我们不会收集使用14周岁以下(含14周岁)未成年人个人信息,且不会利用其信息推送新闻、时政信息、广告等定向推送活动。";
|
|
if (!UserUtil.isAgreeUserProtocol(getApplicationContext())) {
|
new UserProtocolDialog.Builder(this).setNegativeButton("不同意", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
SplashActivity.this.finish();
|
}
|
}).setPositiveButton("同意并继续", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
dialog.dismiss();
|
UserUtil.agreeUserProtocol(getApplicationContext());
|
//进行下一步
|
if (Build.VERSION.SDK_INT >= 23) {
|
checkAndRequestPermission();
|
} else {
|
getAdId();
|
}
|
}
|
}).setData(data).create().show();
|
} else {
|
//进行下一步
|
if (Build.VERSION.SDK_INT >= 23) {
|
checkAndRequestPermission();
|
} else {
|
getAdId();
|
}
|
}
|
}
|
|
|
private String getChannel() throws NameNotFoundException {
|
return "360";
|
}
|
|
private void getAdId() {
|
final SharedPreferences preferences = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String channel = "";
|
try {
|
channel = getChannel();
|
} catch (NameNotFoundException e1) {
|
e1.printStackTrace();
|
}
|
MGVideoAPI.getAdId(this, channel, new DeviceUuidFactory(this)
|
.getDeviceUuid().toString(), preferences.getString("imei", ""),
|
preferences.getString("mac", ""), "", "",
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
if (Boolean.parseBoolean(jsonObject.optJSONObject(
|
"Data").optString("showAd"))) {
|
BeibeiConstant.ADSHOW = true;
|
requestAd();
|
} else {
|
BeibeiConstant.ADSHOW = false;
|
toMainActivity();
|
}
|
} else {
|
requestAd();
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString,
|
throwable);
|
requestAd();
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
}
|
});
|
}
|
|
private void requestAd() {
|
fl_ad.postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
loadSplashAd(AdTypeEnum.csj, CSJConstant.CODE_SPLASH);
|
}
|
}, 500);
|
}
|
|
|
/**
|
* 加载开屏广告
|
*
|
* @param adType
|
*/
|
private void loadSplashAd(final AdTypeEnum adType, final String pid) {
|
SplashAdUtil.loadAD(pid, adType, SplashActivity.this, null, fl_ad, null, new SplashAdUtil.SplashAdListener() {
|
@Override
|
public void close() {
|
runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
toMainActivity();
|
}
|
});
|
}
|
|
@Override
|
public void noAd() {//初次加载未加载出广告
|
if (AdTypeEnum.csj == adType) {
|
//再次加载广点通广告
|
SplashAdUtil.loadAD(null, AdTypeEnum.gdt, SplashActivity.this, null, fl_ad, null, new SplashAdUtil.SplashAdListener() {
|
@Override
|
public void close() {
|
runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
toMainActivity();
|
}
|
});
|
}
|
|
@Override
|
public void noAd() {
|
runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
toMainActivity();
|
}
|
});
|
}
|
});
|
|
} else if (AdTypeEnum.gdt == adType) {
|
//再次加载穿山甲广告
|
//再次加载广点通广告
|
SplashAdUtil.loadAD(null, AdTypeEnum.csj, SplashActivity.this, null, fl_ad, null, new SplashAdUtil.SplashAdListener() {
|
@Override
|
public void close() {
|
runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
toMainActivity();
|
}
|
});
|
}
|
|
@Override
|
public void noAd() {
|
runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
toMainActivity();
|
}
|
});
|
}
|
});
|
|
} else {
|
runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
toMainActivity();
|
}
|
});
|
}
|
}
|
});
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
MobclickAgent.onPageStart("欢迎页");
|
// new WebView(this);
|
mList = new ArrayList<Contacts>();
|
// if (!getSharedPreferences("user", MODE_PRIVATE).getBoolean(
|
// "updateMobile", false)) {
|
// getPhoneContacts();
|
// }
|
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
MobclickAgent.onPageEnd("欢迎页");
|
}
|
|
/**
|
* 获取库Phon表字段
|
**/
|
private static final String[] PHONES_PROJECTION = new String[]{
|
Phone.DISPLAY_NAME, Phone.NUMBER};
|
/**
|
* 联系人显示名称
|
**/
|
private static final int PHONES_DISPLAY_NAME_INDEX = 0;
|
|
/**
|
* 电话号码
|
**/
|
private static final int PHONES_NUMBER_INDEX = 1;
|
private List<Contacts> mList;
|
|
/**
|
* 得到手机通讯录联系人信息
|
**/
|
private void getPhoneContacts() {
|
ContentResolver resolver = getContentResolver();
|
|
// 获取手机联系人
|
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
|
PHONES_PROJECTION, null, null, null);
|
|
if (phoneCursor != null) {
|
while (phoneCursor.moveToNext()) {
|
|
// 得到手机号码
|
String contactNumber = phoneCursor.getString(
|
PHONES_NUMBER_INDEX).replace(" ", "");
|
// 当手机号码为空的或者为空字段 跳过当前循环
|
if (TextUtils.isEmpty(contactNumber))
|
continue;
|
|
// 得到联系人名称
|
String contactName = phoneCursor
|
.getString(PHONES_DISPLAY_NAME_INDEX);
|
if (StringUtils.isMobileNumber(contactNumber)) {
|
Contacts info = new Contacts();
|
info.setName(contactName);
|
info.setMobile(contactNumber);
|
mList.add(info);
|
}
|
|
}
|
phoneCursor.close();
|
}
|
getSIMContacts();
|
}
|
|
/**
|
* 得到手机SIM卡联系人人信息
|
**/
|
private void getSIMContacts() {
|
ContentResolver resolver = getContentResolver();
|
// 获取Sims卡联系人
|
Uri uri = Uri.parse("content://icc/adn");
|
Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,
|
null);
|
|
if (phoneCursor != null) {
|
while (phoneCursor.moveToNext()) {
|
|
// 得到手机号码
|
String contactNumber = phoneCursor.getString(
|
PHONES_NUMBER_INDEX).replace(" ", "");
|
// 当手机号码为空的或者为空字段 跳过当前循环
|
if (TextUtils.isEmpty(contactNumber))
|
continue;
|
// 得到联系人名称
|
String contactName = phoneCursor
|
.getString(PHONES_DISPLAY_NAME_INDEX);
|
|
if (StringUtils.isMobileNumber(contactNumber)) {
|
Contacts info = new Contacts();
|
info.setName(contactName);
|
info.setMobile(contactNumber);
|
mList.add(info);
|
}
|
}
|
}
|
updateContact();
|
|
}
|
|
private void updateContact() {
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
String uid = sp.getString("uid", "");
|
Gson gson = new GsonBuilder().setPrettyPrinting()
|
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
|
.create();
|
String str = Base64.encodeToString(gson.toJson(mList).getBytes(),
|
Base64.DEFAULT);
|
MGVideoAPI.updateContact(this, uid, str,
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
|
if (jsonObject.optBoolean("IsPost")) {
|
SharedPreferences sp = getSharedPreferences("user",
|
MODE_PRIVATE);
|
Editor editor = sp.edit();
|
editor.putBoolean("updateMobile", true);
|
editor.commit();
|
}
|
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString,
|
throwable);
|
}
|
});
|
}
|
|
private String getDay(long time) {
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
String date = format.format(new Date(time));
|
return date;
|
}
|
|
@Override
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
if (keyCode == KeyEvent.KEYCODE_BACK)
|
return false;
|
else
|
return super.onKeyDown(keyCode, event);
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
// Mvad.activityDestroy(SplashActivity.this);
|
}
|
|
private final class DisplayNextView implements Animation.AnimationListener {
|
ImageView iv;
|
|
public DisplayNextView(ImageView iv) {
|
this.iv = iv;
|
}
|
|
public void onAnimationStart(Animation animation) {
|
}
|
|
// 动画结束
|
public void onAnimationEnd(Animation animation) {
|
toMainActivity();
|
}
|
|
public void onAnimationRepeat(Animation animation) {
|
}
|
}
|
|
/*
|
* 动画
|
*/
|
private void showAnimation(float start, float end, ImageView iv) {
|
Animation animation = null;
|
// animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
|
// Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
|
// 0.5f);
|
animation = new AlphaAnimation(0.1f, 1.0f);
|
animation.setFillAfter(true);
|
// animation.setInterpolator(new DecelerateInterpolator());
|
animation.setDuration(1500);
|
animation.setAnimationListener(new DisplayNextView(iv));
|
iv.startAnimation(animation);
|
|
}
|
|
boolean isFinish = false;
|
|
private void toMainActivity() {
|
if (!isFinish) {
|
isFinish = true;
|
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
|
startActivity(intent);
|
finish();
|
}
|
}
|
|
private void toLinkPage() {
|
if (!isFinish) {
|
isFinish = true;
|
Intent intent = new Intent(SplashActivity.this,
|
LinkPageActivity.class);
|
startActivity(intent);
|
finish();
|
}
|
}
|
|
/**
|
* ----------非常重要----------
|
* <p>
|
* Android6.0以上的权限适配简单示例:
|
* <p>
|
* 如果targetSDKVersion >= 23,那么必须要申请到所需要的权限,再调用广点通SDK,否则广点通SDK不会工作。
|
* <p>
|
* Demo代码里是一个基本的权限申请示例,请开发者根据自己的场景合理地编写这部分代码来实现权限申请。
|
* 注意:下面的`checkSelfPermission`和`requestPermissions`方法都是在Android6.0的SDK中增加的API,如果您的App还没有适配到Android6.0以上,则不需要调用这些方法,直接调用广点通SDK即可。
|
*/
|
@TargetApi(Build.VERSION_CODES.M)
|
private void checkAndRequestPermission() {
|
List<String> lackedPermission = new ArrayList<String>();
|
if (!(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)) {
|
lackedPermission.add(Manifest.permission.READ_PHONE_STATE);
|
}
|
|
if (!(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
|
lackedPermission.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
}
|
|
if (lackedPermission.size() != 0) {
|
// 请求所缺少的权限,在onRequestPermissionsResult中再看是否获得权限,如果获得权限就可以调用SDK,否则不要调用SDK。
|
String[] requestPermissions = new String[lackedPermission.size()];
|
lackedPermission.toArray(requestPermissions);
|
requestPermissions(requestPermissions, 1024);
|
} else {
|
getAdId();
|
}
|
}
|
|
private boolean hasAllPermissionsGranted(int[] grantResults) {
|
for (int grantResult : grantResults) {
|
if (grantResult == PackageManager.PERMISSION_DENIED) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
@Override
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
if (requestCode == 1024) {
|
if (!hasAllPermissionsGranted(grantResults)) {
|
Toast.makeText(this, "应用缺少必要的权限!请点击\"权限\",打开所需要的权限。", Toast.LENGTH_LONG).show();
|
// 如果用户没有授权,那么应该说明意图,引导用户去设置里面授权。
|
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
intent.setData(Uri.parse("package:" + getPackageName()));
|
startActivity(intent);
|
} else {
|
getAdId();
|
}
|
}
|
}
|
}
|