riku
2020-10-10 5771916ffb24807dd57c1969baa6371611c334da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package cn.flightfeather.thirdapp.activity;
 
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import com.bm.library.PhotoView;
import com.ping.greendao.gen.MediafileDao;
 
import java.io.File;
import java.util.List;
 
import cn.flightfeather.thirdapp.CommonApplication;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.adapter.CommonPagerAdapter;
import cn.flightfeather.thirdapp.bean.Mediafile;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.http.DELETE;
import retrofit2.http.Path;
 
public class PhotoViewerActivity extends AppCompatActivity implements View.OnClickListener{
    private PhotoView pv_main;
    private TextView tv_name;
    private ImageView iv_delete;
    private ImageView iv_share;
    private TextView tv_currentP;
    private TextView tv_totalCount;
    private ViewPager view_pager;
    private  int type;
    private boolean deletable;
    private  File file;
    private List<File> files;
    private List<Mediafile> mediafiles;
    private Mediafile mediafile;
    private int position;
    public static final  int CAMERA_PHOTO = 10;
    public static final int CAMERA_PHOTO_TEMP =14;
    public static final int SIGN_PHOTO =11;
    public static final int EVIDENCE_PHOTO = 12;
    public static final int EVIDENCE_PHOTO_TEMP =16;
    public static final int GIT_PHOTO =15;
    public static final int GIT_PHOTO_TEMP=13;
    public static final int PROBLEM_PHOTO =17;
    public static final int CHANGE_PHOTO =18;
 
    public static final String PARA_FILES = "files";
    public static final String PARA_MEDIAS = "mediaFiles";
 
    private CommonApplication application;
    private PhotoViewerService photoViewerService;
    private MediafileDao mediafileDao;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_viewer);
        application = (CommonApplication) getApplication();
        initUI();
        initData2();
 
    }
    //初始化界面
    private void initUI() {
        tv_currentP = findViewById(R.id.tv_currentP);
        tv_totalCount = findViewById(R.id.tv_totalCount);
        tv_name = (TextView) findViewById(R.id.tv_name);
        iv_delete = (ImageView) findViewById(R.id.iv_delete);
        iv_share = (ImageView) findViewById(R.id.iv_share);
        view_pager = findViewById(R.id.view_pager);
 
    }
    //初始化数据
//    private void initData() {
//        photoViewerService = (PhotoViewerService) application.getRetrofit().create(PhotoViewerService.class);
//        mediafileDao = application.getDaoSession().getMediafileDao();
//
//        Intent intent = getIntent();
//        file = (File) intent.getSerializableExtra("file");
//        type = intent.getIntExtra("type",0);
//        deletable = intent.getBooleanExtra("deletable",false);
//
//
//
//        if (file!=null){
//            Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
//            pv_main.setImageBitmap(bm);
//
//            String name = file.getName();
//            if (name.length()>8){
//                tv_name.setTitle(name.substring(0,name.length()-8));
//            }else {
//                tv_name.setTitle(name);
//            }
//
//
//            iv_share.setOnClickListener(this);
//            if (!deletable){
//                iv_delete.setVisibility(View.GONE);
//            }else {
//                mediafile = (Mediafile) intent.getSerializableExtra("mediaFile");
//                if (mediafile ==null&&type!= GIT_PHOTO_TEMP&&type!=CAMERA_PHOTO_TEMP&&type!=EVIDENCE_PHOTO_TEMP){
//                    Toast.makeText(application, "mediaFile获取失败", Toast.LENGTH_SHORT).show();
//                    iv_delete.setVisibility(View.GONE);
//                }
//                position = intent.getIntExtra("position",-1);
//                iv_delete.setOnClickListener(this);
//            }
//
//        }
//    }
 
    private void initData2() {
        photoViewerService = (PhotoViewerService) application.getRetrofit().create(PhotoViewerService.class);
        mediafileDao = application.getDaoSession().getMediafileDao();
 
        Intent intent = getIntent();
        files = (List<File>) intent.getSerializableExtra(PARA_FILES);
        type = intent.getIntExtra("type",0);
        deletable = intent.getBooleanExtra("deletable",false);
        mediafiles = (List<Mediafile>) intent.getSerializableExtra(PARA_MEDIAS);
        position = intent.getIntExtra("position",-1);
 
        iv_share.setOnClickListener(this);
 
        if (!deletable){
            iv_delete.setVisibility(View.GONE);
        }else {
            if (mediafiles ==null&&type!= GIT_PHOTO_TEMP&&type!=CAMERA_PHOTO_TEMP&&type!=EVIDENCE_PHOTO_TEMP){
                Toast.makeText(application, "mediaFile获取失败", Toast.LENGTH_SHORT).show();
                iv_delete.setVisibility(View.GONE);
            }
            iv_delete.setOnClickListener(this);
        }
 
        if (files != null && files.size() > 0) {
            tv_currentP.setText(String.valueOf(position + 1));
            tv_totalCount.setText(String.valueOf(files.size()));
 
            String name = files.get(position).getName();
            if (name.length() > 8) {
                tv_name.setText(name.substring(0, name.length() - 8));
            } else {
                tv_name.setText(name);
            }
 
            view_pager.setAdapter(new CommonPagerAdapter(files, this));
            view_pager.setCurrentItem(position);
            view_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int i, float v, int i1) {
 
                }
 
                @Override
                public void onPageSelected(int i) {
                    String name = files.get(i).getName();
                    if (name.length() > 8) {
                        tv_name.setText(name.substring(0, name.length() - 8));
                    } else {
                        tv_name.setText(name);
                    }
                    position = i;
                    tv_currentP.setText(String.valueOf(i + 1));
                }
 
                @Override
                public void onPageScrollStateChanged(int i) {
 
                }
            });
        }
    }
 
    //分享单张图片
    private void shareSingPhoto(File file){
        //分享图片
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
        Uri imageUri = Uri.fromFile(file);
//        Uri imageUri = FileProvider.getUriForFile(getApplication(), getApplicationContext().getPackageName() + ".fileProvider", file);
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM,imageUri);
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent, "分享一张图片"));
    }
    //显示删除的dialog
    private void showDeleteDialog(){
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("要删除这张照片吗?");
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                    delete(type);
 
            }
        });
        dialog.setNegativeButton("取消",null);
        dialog.show();
    }
 
    private void delete(final int type){
        //postion有值则删除代码写在跳转前actiivty中,否则立即删除
        if (type ==GIT_PHOTO_TEMP||type ==CAMERA_PHOTO_TEMP||type == EVIDENCE_PHOTO_TEMP){
            if (position>-1){
                Intent intent = new Intent();
                intent.putExtra("position",position);
                setResult(RESULT_OK,intent);
                finish();
            }
 
        }else {
            final Mediafile m = mediafiles.get(position);
            if (m.getRemark().equals("未上传")){
                if (type ==CAMERA_PHOTO||type ==SIGN_PHOTO||type ==GIT_PHOTO||type ==PROBLEM_PHOTO||type ==CHANGE_PHOTO){
                    mediafileDao.deleteByKey(m.getGuid());
                    files.get(position).delete();
                    Toast.makeText(application, "删除成功(local)", Toast.LENGTH_SHORT).show();
                    setDeleteResult(type);
                    finish();
                }
 
            }else if (m.getRemark().equals("已上传")){
                Call<ResponseBody> deletePhoto = photoViewerService.deleteMediaFile(m.getGuid());
                deletePhoto.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        if (response.body()!=null){
                            if (type == CAMERA_PHOTO||type == SIGN_PHOTO||type == GIT_PHOTO||type ==PROBLEM_PHOTO||type ==CHANGE_PHOTO){
                                mediafileDao.deleteByKey(m.getGuid());
                                Toast.makeText(application, "删除成功", Toast.LENGTH_SHORT).show();
                                files.get(position).delete();
                                setDeleteResult(type);
                                finish();
                            }
 
                        }else if (response.errorBody()!=null){
                            Toast.makeText(application, "删除失败", Toast.LENGTH_SHORT).show();
                        }
                    }
 
                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        Toast.makeText(application, "联网失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
 
    }
 
    //将要删除的请求返回给上层来删除
    private void  setDeleteResult(int type){
            Intent intent = new Intent();
            intent.putExtra("position",position);
            setResult(RESULT_OK,intent);
    }
 
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_delete:
                showDeleteDialog();
                break;
            case R.id.iv_share:
                shareSingPhoto(files.get(position));
        }
    }
 
    interface PhotoViewerService{
 
        @DELETE("mediafile/{MediaFileGUID}/ ")
        Call<ResponseBody>  deleteMediaFile(@Path("MediaFileGUID") String mediaFileGuid);
    }
}