导航菜单

页面标题

页面副标题

微商客 v2.1.2 - SYMainActivity.java 源代码

正在查看: 微商客 v2.1.2 应用的 SYMainActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.jianzhen.wsk.ui;

import android.app.Activity;
import android.app.RecoverableSecurityException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.MapsInitializer;
import com.bumptech.glide.Glide;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.jianzhen.wsk.BaseActivity;
import com.jianzhen.wsk.R;
import com.jianzhen.wsk.dialog.ShowImgDialog;
import com.jianzhen.wsk.net.ErrorBean;
import com.jianzhen.wsk.net.api.ApiEngine;
import com.jianzhen.wsk.net.api.RequestData;
import com.jianzhen.wsk.net.bean.BaseBean;
import com.jianzhen.wsk.net.bean.WeatherBean;
import com.jianzhen.wsk.net.rxjava.ResultSubscriber;
import com.jianzhen.wsk.utils.SavePicUtils;
import com.jianzhen.wsk.utils.SharedPreferencesUtil;
import com.jianzhen.wsk.utils.Utils;
import com.kongzue.dialogx.dialogs.PopNotification;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SYMainActivity extends BaseActivity implements SurfaceHolder.Callback {
    private static final String[] DAYS_OF_WEEK = {"日", "一", "二", "三", "四", "五", "六"};
    public static final int REQUEST_CODE_DELETE_IMAGE = 1001;
    private ImageView againLocation;
    private boolean isCamera;
    private String locationAddressStr;
    private Camera mCamera;
    private PopNotification mPopNotification;
    private SurfaceHolder mSurfaceHolder;
    private SurfaceView mSurfaceView;
    private ImageView showImg;
    private ImageView showSaveImg;
    private Bitmap takePhoneBitmap;
    private boolean isFirstLocation = true;
    private boolean isTakePhone = false;
    private boolean isLocationAllGranted = false;
    private final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what == 1) {
                WeatherBean weatherBean = (WeatherBean) message.obj;
                if (weatherBean.getStatus().equals("1")) {
                    TextView textView = (TextView) SYMainActivity.this.findViewById(R.id.weather_tv);
                    TextView textView2 = (TextView) SYMainActivity.this.findViewById(R.id.weather_wd);
                    textView.setText(weatherBean.getLives().get(0).getWeather());
                    textView2.setText(weatherBean.getLives().get(0).getTemperature() + "°");
                }
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sy_main;
    }

    @Override
    protected void initTitle() {
    }

    @Override
    protected boolean isSetBaseTitle() {
        return false;
    }

    @Override
    protected void initPageView() {
        this.showImg = (ImageView) findViewById(R.id.w_img);
        this.showSaveImg = (ImageView) findViewById(R.id.show_phone);
        this.mSurfaceView = (SurfaceView) findViewById(R.id.take_sf);
        this.againLocation = (ImageView) findViewById(R.id.again_location);
        boolean isGranted = XXPermissions.isGranted((Context) this, Permission.CAMERA);
        this.isCamera = isGranted;
        if (isGranted) {
            SurfaceHolder holder = this.mSurfaceView.getHolder();
            this.mSurfaceHolder = holder;
            holder.addCallback(this);
            startCameraPreview();
        }
        findViewById(R.id.select_photo_img).setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                SYMainActivity.this.lambda$initPageView$0(view);
            }
        });
        findViewById(R.id.take_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                SYMainActivity.this.lambda$initPageView$1(view);
            }
        });
        findViewById(R.id.save_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                SYMainActivity.this.lambda$initPageView$2(view);
            }
        });
        findViewById(R.id.home_my_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                SYMainActivity.this.lambda$initPageView$3(view);
            }
        });
        this.againLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public final void onClick(View view) {
                SYMainActivity.this.lambda$initPageView$4(view);
            }
        });
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH", Locale.CHINA);
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("mm", Locale.CHINA);
        Calendar calendar = Calendar.getInstance();
        ((TextView) findViewById(R.id.time_tv)).setText(simpleDateFormat.format(date) + ":" + simpleDateFormat2.format(date));
        ((TextView) findViewById(R.id.data_tv)).setText((calendar.get(1) + "-" + (calendar.get(2) + 1) + "-" + calendar.get(5)) + ("\n星期" + DAYS_OF_WEEK[calendar.get(7) - 1]));
        if (XXPermissions.isGranted((Context) this, Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION)) {
            setLocation();
        }
        escalationData();
    }

    public void lambda$initPageView$0(View view) {
        if (this.takePhoneBitmap != null) {
            new ShowImgDialog(this, this.takePhoneBitmap).show();
        } else {
            showCenterToast(this, "请先拍照添加水印!");
        }
    }

    public void lambda$initPageView$1(View view) {
        if (!Utils.isCameraPermission(this)) {
            PopNotification build = PopNotification.build();
            this.mPopNotification = build;
            build.setTitle("相机和存储权限使用说明:").setMessage("用于拍摄、储存照片以及在相册中浏览查看照片").noAutoDismiss().show();
        }
        XXPermissions.with((Context) this).permission(Utils.cameraPermission(this)).request(new OnPermissionCallback() {
            @Override
            public void onGranted(List<String> list, boolean z) {
                if (z) {
                    if (SYMainActivity.this.isCamera) {
                        SYMainActivity sYMainActivity = SYMainActivity.this;
                        sYMainActivity.mSurfaceHolder = sYMainActivity.mSurfaceView.getHolder();
                        SYMainActivity.this.mSurfaceHolder.addCallback(SYMainActivity.this);
                        SYMainActivity.this.startCameraPreview();
                    }
                    SYMainActivity.this.takePhone();
                }
                if (SYMainActivity.this.mPopNotification != null) {
                    SYMainActivity.this.mPopNotification.dismiss();
                }
            }

            @Override
            public void onDenied(List<String> list, boolean z) {
                if (SYMainActivity.this.mPopNotification != null) {
                    SYMainActivity.this.mPopNotification.dismiss();
                }
            }
        });
    }

    public void lambda$initPageView$2(View view) {
        if (!this.isTakePhone) {
            showCenterToast(this, "请先拍照添加水印!");
            return;
        }
        this.againLocation.setVisibility(8);
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.watermark_lay);
        SavePicUtils.getInstance().saveImage29(this, getBitmapFromView(relativeLayout));
        showCenterToast(this, "打卡照片已保存至手机!");
        this.takePhoneBitmap = getBitmapFromView(relativeLayout);
        this.againLocation.setVisibility(0);
        Glide.with(this).load(getBitmapFromView(relativeLayout)).placeholder(R.mipmap.watermark_pic).into(this.showSaveImg);
        SurfaceView surfaceView = this.mSurfaceView;
        if (surfaceView != null) {
            surfaceView.setVisibility(0);
        }
        this.showImg.setVisibility(8);
    }

    public void lambda$initPageView$3(View view) {
        startIntent(this, SYMyActivity.class);
    }

    public void lambda$initPageView$4(View view) {
        if (!XXPermissions.isGranted((Context) this, Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION)) {
            PopNotification build = PopNotification.build();
            this.mPopNotification = build;
            build.setTitle("位置权限使用说明:").setMessage("用于照片显示位置水印").noAutoDismiss().show();
        }
        XXPermissions.with((Context) this).permission(Permission.ACCESS_COARSE_LOCATION).permission(Permission.ACCESS_FINE_LOCATION).request(new OnPermissionCallback() {
            @Override
            public void onGranted(List<String> list, boolean z) {
                if (z) {
                    MapsInitializer.updatePrivacyShow(SYMainActivity.this, true, true);
                    MapsInitializer.updatePrivacyAgree(SYMainActivity.this, true);
                    SYMainActivity.this.isFirstLocation = false;
                    SYMainActivity.this.setLocation();
                }
                if (SYMainActivity.this.mPopNotification != null) {
                    SYMainActivity.this.mPopNotification.dismiss();
                }
            }

            @Override
            public void onDenied(List<String> list, boolean z) {
                if (SYMainActivity.this.mPopNotification != null) {
                    SYMainActivity.this.mPopNotification.dismiss();
                }
            }
        });
    }

    public void takePhone() {
        PictureSelector.create(this).openCamera(SelectMimeType.ofImage()).forResultActivity(new OnResultCallbackListener<LocalMedia>() {
            public void onCancel() {
            }

            public void onResult(ArrayList<LocalMedia> arrayList) {
                SYMainActivity.this.isTakePhone = true;
                SYMainActivity.this.showImg.setVisibility(0);
                if (arrayList == null || arrayList.size() <= 0) {
                    return;
                }
                Glide.with(SYMainActivity.this).load(arrayList.get(0).getPath()).into(SYMainActivity.this.showImg);
            }
        });
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        ContentResolver contentResolver = getContentResolver();
        if (i != 0 || intent == null) {
            return;
        }
        try {
            this.showImg.setImageBitmap(BitmapFactory.decodeStream(contentResolver.openInputStream(intent.getData())));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void setBackground(Uri uri) {
        Drawable drawable;
        try {
            Log.i("图片回调", " uri " + uri);
            drawable = Drawable.createFromStream(getContentResolver().openInputStream(uri), "dataString");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            drawable = null;
        }
        if (drawable != null) {
            this.showImg.setBackground(drawable);
            this.showImg.setVisibility(0);
            Log.d("图片回调", "set ok");
        }
    }

    public void setLocation() {
        final AMapLocationClient aMapLocationClient;
        try {
            aMapLocationClient = new AMapLocationClient(this);
        } catch (Exception e) {
            e.printStackTrace();
            aMapLocationClient = null;
        }
        AMapLocationClientOption aMapLocationClientOption = new AMapLocationClientOption();
        if (aMapLocationClient == null) {
            return;
        }
        aMapLocationClient.setLocationListener(new AMapLocationListener() {
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation != null) {
                    if (aMapLocation.getErrorCode() == 0) {
                        aMapLocation.getLocationType();
                        aMapLocation.getLatitude();
                        aMapLocation.getLongitude();
                        aMapLocation.getAccuracy();
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date(aMapLocation.getTime()));
                        Log.e("dizhi", aMapLocation.getAddress());
                        String str = aMapLocation.getProvince() + aMapLocation.getCity() + aMapLocation.getDistrict() + aMapLocation.getStreet() + aMapLocation.getAoiName();
                        if (!str.isEmpty()) {
                            SYMainActivity.this.locationAddressStr = str;
                        }
                        ((TextView) SYMainActivity.this.findViewById(R.id.address_tv)).setText(SYMainActivity.this.locationAddressStr);
                        aMapLocationClient.stopLocation();
                        aMapLocationClient.onDestroy();
                        if (!SYMainActivity.this.isFirstLocation) {
                            BaseActivity.showCenterToast(SYMainActivity.this, "当前位置已更新!");
                        }
                        SYMainActivity.this.getWeather(aMapLocation.getAdCode());
                        return;
                    }
                    Log.e("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo());
                }
            }
        });
        aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        aMapLocationClientOption.setInterval(2000L);
        aMapLocationClient.setLocationOption(aMapLocationClientOption);
        aMapLocationClient.startLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public Bitmap getBitmapFromView(View view) {
        Bitmap createBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        view.layout(0, 0, view.getWidth(), view.getHeight());
        Log.d("ss", "combineImages: width: " + view.getWidth());
        Log.d("ss", "combineImages: height: " + view.getHeight());
        view.draw(canvas);
        view.requestLayout();
        return createBitmap;
    }

    public void startCameraPreview() {
        try {
            Camera open = Camera.open();
            this.mCamera = open;
            open.setPreviewDisplay(this.mSurfaceHolder);
            this.mCamera.setDisplayOrientation(90);
            this.mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stopCameraPreview() {
        this.mCamera.stopPreview();
        this.mCamera.release();
        this.mCamera = null;
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        startCameraPreview();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
        if (this.mSurfaceHolder.getSurface() == null) {
            return;
        }
        stopCameraPreview();
        startCameraPreview();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        stopCameraPreview();
    }

    public void getWeather(String str) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Response execute = new OkHttpClient().newCall(new Request.Builder().url("https://restapi.amap.com/v3/weather/weatherInfo?ip=113.218.20.251&key=198b7c3ef3888960f09b2d9ab18fc04c").build()).execute();
                    String str2 = "";
                    if (execute.body() != null) {
                        str2 = execute.body().string();
                    }
                    WeatherBean weatherBean = (WeatherBean) JSON.parseObject(str2, WeatherBean.class);
                    Message message = new Message();
                    message.what = 1;
                    message.obj = weatherBean;
                    SYMainActivity.this.handler.sendMessage(message);
                    Log.e("responseDataxxx", str2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void deleteImage(Activity activity, Uri uri) {
        if (Build.VERSION.SDK_INT < 29) {
            return;
        }
        try {
            activity.getContentResolver().delete(uri, null, null);
        } catch (Exception e) {
            if (Build.VERSION.SDK_INT >= 29) {
                if (!(e instanceof RecoverableSecurityException)) {
                    throw new RuntimeException(e.getMessage(), e);
                }
                try {
                    activity.startIntentSenderForResult(((RecoverableSecurityException) e).getUserAction().getActionIntent().getIntentSender(), 1001, null, 0, 0, 0, null);
                    return;
                } catch (IntentSender.SendIntentException e2) {
                    e2.printStackTrace();
                    return;
                }
            }
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    private void escalationData() {
        if (!SharedPreferencesUtil.getBoolean(this, "new_add_escalation", false)) {
            escalationDataRequest("1");
        }
        if (SharedPreferencesUtil.getString(this, "today_active_escalation", "").equals(Utils.getCurrentDate())) {
            return;
        }
        escalationDataRequest("4");
    }

    public void escalationDataRequest(final String str) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new RequestData.RequestPar("type", str));
        ApiEngine.getInstance().getApiService().escalation(RequestData.get(arrayList)).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ResultSubscriber<BaseBean>() {
            @Override
            public void onError(ErrorBean errorBean) {
            }

            @Override
            public void onData(BaseBean baseBean) {
                if (baseBean.getCode() == 200) {
                    if (str.equals("1")) {
                        SharedPreferencesUtil.putBoolean(SYMainActivity.this, "new_add_escalation", true);
                    } else if (str.equals("4")) {
                        SharedPreferencesUtil.putString(SYMainActivity.this, "today_active_escalation", Utils.getCurrentDate());
                    }
                }
            }
        });
    }
}