Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.13 KB | None | 0 0
  1. package webcam.apps.android.freemium.com.webcam;
  2.  
  3. import android.Manifest;
  4. import android.app.ActionBar;
  5. import android.app.Dialog;
  6. import android.app.ProgressDialog;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.content.pm.ActivityInfo;
  11. import android.content.pm.PackageManager;
  12. import android.graphics.Bitmap;
  13. import android.graphics.Color;
  14. import android.net.Uri;
  15. import android.os.AsyncTask;
  16. import android.os.Build;
  17. import android.os.Bundle;
  18. import android.os.Environment;
  19. import android.os.NetworkOnMainThreadException;
  20. import android.preference.PreferenceManager;
  21. import android.support.annotation.NonNull;
  22. import android.support.v4.app.ActivityCompat;
  23. import android.support.v4.content.ContextCompat;
  24. import android.support.v7.app.AlertDialog;
  25. import android.support.v7.app.AppCompatActivity;
  26. import android.support.v7.widget.Toolbar;
  27. import android.util.Log;
  28. import android.view.View;
  29. import android.view.ViewTreeObserver;
  30. import android.widget.Button;
  31. import android.widget.ImageView;
  32. import android.widget.LinearLayout;
  33. import android.widget.ProgressBar;
  34. import android.widget.RelativeLayout;
  35. import android.widget.TextView;
  36. import android.widget.Toast;
  37. import android.widget.ToggleButton;
  38. import com.github.niqdev.mjpeg.DisplayMode;
  39. import com.github.niqdev.mjpeg.Mjpeg;
  40. import com.github.niqdev.mjpeg.MjpegView;
  41. import com.github.niqdev.mjpeg.OnFrameCapturedListener;
  42. import com.github.rahatarmanahmed.cpv.CircularProgressView;
  43. import com.google.android.gms.ads.AdRequest;
  44. import com.google.android.gms.ads.AdView;
  45. import com.google.android.gms.ads.MobileAds;
  46. import com.squareup.picasso.Callback;
  47. import com.squareup.picasso.MemoryPolicy;
  48. import com.squareup.picasso.Picasso;
  49. import org.json.JSONObject;
  50. import java.io.BufferedReader;
  51. import java.io.File;
  52. import java.io.FileOutputStream;
  53. import java.io.IOException;
  54. import java.io.InputStream;
  55. import java.io.InputStreamReader;
  56. import java.net.HttpURLConnection;
  57. import java.net.MalformedURLException;
  58. import java.net.URL;
  59. import java.text.SimpleDateFormat;
  60. import java.util.ArrayList;
  61. import java.util.Date;
  62. import java.util.concurrent.atomic.AtomicBoolean;
  63. import webcam.apps.android.freemium.com.webcam.Enum.CameraType;
  64. import webcam.apps.android.freemium.com.webcam.Utils.CameraModel;
  65. import webcam.apps.android.freemium.com.webcam.Utils.CameraTypeGenerator;
  66. import webcam.apps.android.freemium.com.webcam.Utils.FileTool;
  67. import webcam.apps.android.freemium.com.webcam.Utils.InterstitialHelper;
  68. import webcam.apps.android.freemium.com.webcam.Utils.CameraAdapter;
  69. import webcam.apps.android.freemium.com.webcam.Utils.CameraDatabaseHelper;
  70. import webcam.apps.android.freemium.com.webcam.Utils.CameraHelper;
  71. import webcam.apps.android.freemium.com.webcam.Utils.WebService;
  72.  
  73. /**
  74. * Created by tymoteusz on 05.08.17.
  75. */
  76.  
  77. public class CameraActivity extends AppCompatActivity implements View.OnClickListener,
  78. OnFrameCapturedListener {
  79.  
  80. private final AtomicBoolean loaded = new AtomicBoolean();
  81. private final int TIMEOUT = 5;
  82. private final int MEDIA_PERMISSION_REQUEST_CODE = 100;
  83.  
  84. private final String KEY_URL = "url";
  85. private final String KEY_COUNTRY = "country";
  86. private final String KEY_REGION = "region";
  87. private final String KEY_CATEGORY = "category";
  88. private final String KEY_CITY = "city";
  89. private final String URL_TYPE_GENERATOR = "urlTypeGenerator";
  90. private final String KEY_CLASS_NAME = "className";
  91.  
  92. private String url = "";
  93. private String category = "";
  94. private String region = "";
  95. private String city = "";
  96. private String country = "";
  97. private String type;
  98. private ImageView imageView;
  99. private ImageView imageViewSecond;
  100. private ImageView imageNoCam;
  101.  
  102. private InterstitialHelper interstitialHelper;
  103. private Toolbar toolbar;
  104. private MjpegView MJPEGViewSmall;
  105. private LinearLayout MJPGLayout;
  106. private LinearLayout imagesLayout;
  107.  
  108. private LinearLayout secondButton;
  109. private LinearLayout firstButton;
  110.  
  111. private boolean isFinished;
  112. private boolean isFavourite = false;
  113. private boolean isWarned = false;
  114. private boolean isLiked = false;
  115. private boolean isCameraActive = false;
  116. private boolean isMJPG = false;
  117. private boolean activeCameraImageView;
  118.  
  119. private CameraHelper cameraHelperInstance = CameraHelper.getInstance();
  120. private CameraDatabaseHelper cameraDatabaseHelper;
  121. private ArrayList<CameraModel> cameraListFavDB;
  122. private CameraAdapter cameraAdapter;
  123.  
  124. private com.github.clans.fab.FloatingActionButton addToFavFAB;
  125. private com.github.clans.fab.FloatingActionButton warnFab;
  126. private com.github.clans.fab.FloatingActionMenu floatingActionButton;
  127.  
  128. public SharedPreferences sharedPreferences;
  129.  
  130. private ToggleButton likeTb;
  131. private TextView categoryTv;
  132. private TextView categoryTxtTv;
  133. private TextView likeTxtTv;
  134.  
  135. public FileTool fileTool;
  136.  
  137. private CircularProgressView progressBarCam;
  138.  
  139. private ProgressDialog pd;
  140. private ProgressBar likeProgressBar;
  141.  
  142. private ArrayList<CameraModel> likeList;
  143. private ArrayList<CameraModel> warnList;
  144.  
  145. private Bitmap lastPreview = null;
  146.  
  147. private Mjpeg mjpeg;
  148.  
  149. @Override
  150. protected void onCreate(Bundle savedInstanceState) {
  151. super.onCreate(savedInstanceState);
  152. setContentView(R.layout.camera_layout);
  153. initDatabase();
  154. setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
  155. advert();
  156. initToolbar();
  157. initWidgets();
  158. initHelpers();
  159. addView();
  160. getLikesFromServer();
  161. createFolder();
  162. checkCameraType();
  163. }
  164.  
  165. private void checkValueOfCategory(){
  166. if(category != null) {
  167. if (category.contains("None") || category.contains("")) {
  168. categoryTv.setVisibility(View.GONE);
  169. categoryTxtTv.setVisibility(View.GONE);
  170. }
  171. } else {
  172. categoryTv.setVisibility(View.GONE);
  173. categoryTxtTv.setVisibility(View.GONE);
  174. }
  175. }
  176.  
  177. @Override
  178. public void onFrameCaptured(Bitmap bitmap) {
  179. lastPreview = bitmap;
  180. }
  181.  
  182. private void cleanActivityMJPG(){
  183. class MjpgClean extends AsyncTask<String, String, String> {
  184. protected void onPreExecute() {
  185. super.onPreExecute();
  186. }
  187.  
  188. protected String doInBackground(String... params) {
  189. try {
  190. MJPEGViewSmall.stopPlayback();
  191. } catch (NullPointerException e) {
  192. e.printStackTrace();
  193. }
  194. return null;
  195. }
  196.  
  197. @Override
  198. protected void onPostExecute(String result) {
  199. super.onPostExecute(result);
  200. }
  201. }
  202. MjpgClean mjpgClean = new MjpgClean();
  203. mjpgClean.execute();
  204. }
  205.  
  206. @Override
  207. protected void onPause() {
  208. super.onPause();
  209. try {
  210. if(MJPEGViewSmall.isStreaming()) {
  211. Thread thread = new Thread(() -> {
  212. try {
  213. MJPEGViewSmall.stopPlayback();
  214. } catch (Exception e) {
  215. e.printStackTrace();
  216. }
  217. });
  218. thread.start();
  219. }
  220. } catch (NullPointerException e) {
  221. e.printStackTrace();
  222. }
  223. }
  224.  
  225. @Override
  226. protected void onDestroy() {
  227. isFinished = true;
  228. super.onDestroy();
  229. }
  230.  
  231. @Override
  232. protected void onResume() {
  233. super.onResume();
  234. checkCameraType();
  235. firstButton.setEnabled(true);
  236. }
  237.  
  238. public void setSupportActionBar(Toolbar toolbar) {
  239. getDelegate().setSupportActionBar(toolbar);
  240. toolbar.setNavigationIcon(R.drawable.ico_left);
  241. toolbar.setTitle(R.string.camera_activity_name);
  242. toolbar.setNavigationOnClickListener(view -> {
  243. onBackPressed();
  244. });
  245. }
  246.  
  247. @Override
  248. public void onBackPressed() {
  249. super.onBackPressed();
  250. }
  251.  
  252. @Override
  253. protected void onStop() {
  254. super.onStop();
  255. }
  256.  
  257. @Override
  258. public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
  259. @NonNull int[] grantResults) {
  260. switch (requestCode) {
  261. case MEDIA_PERMISSION_REQUEST_CODE:
  262. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  263. Picture picture = new Picture();
  264. picture.execute();
  265. }
  266. break;
  267. }
  268. }
  269. @Override
  270. public void onClick(View v) {
  271. switch (v.getId()) {
  272. case R.id.add_to_fav_fab:
  273. interstitialHelper.checkInterstitialCounter();
  274. if (!isFavourite) {
  275. addToFavDb();
  276. isFavourite = true;
  277. } else {
  278. removeFromDB();
  279. isFavourite = false;
  280. }
  281. break;
  282. case R.id.warn_fab:
  283. interstitialHelper.checkInterstitialCounter();
  284. if (!isWarned) {
  285. onReportCamera(this);
  286. }
  287. break;
  288. case R.id.snapshot:
  289. if(isCameraActive){
  290. if (ActivityCompat.checkSelfPermission(CameraActivity.this,
  291. Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  292. ActivityCompat.requestPermissions(CameraActivity.this,
  293. new String[]{
  294. Manifest.permission.WRITE_EXTERNAL_STORAGE,
  295. Manifest.permission.WRITE_EXTERNAL_STORAGE},
  296. MEDIA_PERMISSION_REQUEST_CODE);
  297. } else {
  298. Picture picture = new Picture();
  299. Log.d("GetCurrentImage","Image: " + cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
  300. if(MJPEGViewSmall != null || imageView != null || imageViewSecond != null) {
  301. picture.execute();
  302. firstButton.setEnabled(false);
  303. } else {
  304. CharSequence txt = getString(R.string.error_while_saving);
  305. Toast toast = Toast.makeText(this, txt, Toast.LENGTH_SHORT);
  306. toast.show();
  307. }
  308. }
  309. } else {
  310. CharSequence txt = getString(R.string.error_while_saving);
  311. Toast toast = Toast.makeText(this, txt, Toast.LENGTH_SHORT);
  312. toast.show();
  313. }
  314. break;
  315. case R.id.history:
  316. goToHistory();
  317. break;
  318. case R.id.fullscreen:
  319. goToFullScreen();
  320. break;
  321. case R.id.first:
  322. if(isCameraActive){
  323. if (ActivityCompat.checkSelfPermission(CameraActivity.this,
  324. Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  325. ActivityCompat.requestPermissions(CameraActivity.this,
  326. new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
  327. Manifest.permission.WRITE_EXTERNAL_STORAGE},
  328. MEDIA_PERMISSION_REQUEST_CODE);
  329. } else {
  330. firstButton.setEnabled(false);
  331. Picture picture = new Picture();
  332. Log.d("GetCurrentImage","Image: " + cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
  333. if(MJPEGViewSmall != null || imageView != null || imageViewSecond != null) {
  334. picture.execute();
  335. firstButton.setEnabled(false);
  336. } else {
  337. CharSequence txt = getString(R.string.error_while_saving);
  338. Toast toast = Toast.makeText(this, txt, Toast.LENGTH_SHORT);
  339. toast.show();
  340. firstButton.setEnabled(true);
  341. }
  342. }
  343. } else {
  344. CharSequence txt = getString(R.string.error_while_saving);
  345. Toast toast = Toast.makeText(this, txt, Toast.LENGTH_SHORT);
  346. toast.show();
  347. }
  348. break;
  349. case R.id.second:
  350. goToHistory();
  351. break;
  352. case R.id.third:
  353. goToFullScreen();
  354. break;
  355. case R.id.like:
  356. interstitialHelper.checkInterstitialCounter();
  357. if (!isLiked) {
  358. addLike();
  359. isLiked = true;
  360. } else {
  361. removeLike();
  362. isLiked = false;
  363. }
  364. break;
  365. }
  366. }
  367.  
  368. private void goToHistory() {
  369. class HistoryClean extends AsyncTask<String, String, String> {
  370. protected void onPreExecute() {
  371. super.onPreExecute();
  372. secondButton.setEnabled(false);
  373. }
  374.  
  375. protected String doInBackground(String... params) {
  376. try {
  377. if (MJPEGViewSmall.isStreaming()) {
  378. MJPEGViewSmall.stopPlayback();
  379. }
  380. } catch(Exception e){
  381. e.printStackTrace();
  382. }
  383. return null;
  384. }
  385.  
  386. @Override
  387. protected void onPostExecute(String result) {
  388. super.onPostExecute(result);
  389. Intent intentFullScreen = new Intent(CameraActivity.this, HistoryActivity.class);
  390. intentFullScreen.putExtra(KEY_URL, url);
  391. intentFullScreen.putExtra(KEY_URL, url);
  392. intentFullScreen.putExtra(KEY_CATEGORY, category);
  393. intentFullScreen.putExtra(KEY_REGION, region);
  394. intentFullScreen.putExtra(KEY_COUNTRY, country);
  395. intentFullScreen.putExtra(KEY_CITY, city);
  396. intentFullScreen.putExtra(KEY_CLASS_NAME, "camera");
  397. intentFullScreen.putExtra(URL_TYPE_GENERATOR, type);
  398. startActivity(intentFullScreen);
  399. secondButton.setEnabled(true);
  400. CameraActivity.this.onStop();
  401. }
  402. }
  403. HistoryClean historyClean = new HistoryClean();
  404. historyClean.execute();
  405. }
  406.  
  407. private void goToFullScreen(){
  408. class MjpgClean extends AsyncTask<String, String, String> {
  409. protected void onPreExecute() {
  410. super.onPreExecute();
  411. }
  412.  
  413. protected String doInBackground(String... params) {
  414. /*try {
  415. if (MJPEGViewSmall.isStreaming()) {
  416. MJPEGViewSmall.stopPlayback();
  417. }
  418. } catch(Exception e){
  419. e.printStackTrace();
  420. }*/
  421. return null;
  422. }
  423.  
  424. @Override
  425. protected void onPostExecute(String result) {
  426. super.onPostExecute(result);
  427. Intent intentFullScreen = new Intent(CameraActivity.this, FullScreenActivity.class);
  428. intentFullScreen.putExtra(KEY_URL, url);
  429. intentFullScreen.putExtra(KEY_CATEGORY, category);
  430. intentFullScreen.putExtra(KEY_REGION, region);
  431. intentFullScreen.putExtra(KEY_COUNTRY, country);
  432. intentFullScreen.putExtra(KEY_CITY, city);
  433. intentFullScreen.putExtra(URL_TYPE_GENERATOR, type);
  434. startActivity(intentFullScreen);
  435. }
  436. }
  437. MjpgClean mjpgClean = new MjpgClean();
  438. mjpgClean.execute();
  439. }
  440.  
  441. private void addView(){
  442. WebService webService = new WebService();
  443. webService.postViewServer(cameraHelperInstance.getCurrentCameraModel().getId());
  444. }
  445.  
  446. private void addLike(){
  447. likeTb.setBackgroundDrawable(getResources().getDrawable(R.drawable.ico_like_on));
  448. isLiked = true;
  449. likeTxtTv.setText(String.valueOf(Integer.parseInt(likeTxtTv.getText().toString()) + 1));
  450. cameraDatabaseHelper.addLike(cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
  451. WebService webService = new WebService();
  452. webService.postLikeServer(cameraHelperInstance.getCurrentCameraModel().getId());
  453. }
  454.  
  455. private void removeLike(){
  456. interstitialHelper.checkInterstitialCounter();
  457. likeTb.setBackgroundDrawable(getResources().getDrawable(R.drawable.ico_like_off));
  458. isLiked = false;
  459. long s = Integer.parseInt(likeTxtTv.getText().toString()) - 1;
  460. if(s == -1)
  461. s = 0;
  462. likeTxtTv.setText(String.valueOf(s));
  463. cameraDatabaseHelper.removeFromLike(
  464. cameraHelperInstance.getCurrentCameraModel().getCameraUrl(), this);
  465. WebService webService = new WebService();
  466. webService.postRemoveLikeServer(cameraHelperInstance.getCurrentCameraModel().getId());
  467. }
  468.  
  469. private void getLikesFromServer(){
  470. class CameraInfoAsyncTask extends AsyncTask<String, String, String> {
  471. @Override
  472. protected void onPreExecute() {
  473. super.onPreExecute();
  474. }
  475. @Override
  476. protected String doInBackground(String... params) {
  477. HttpURLConnection connection = null;
  478. BufferedReader reader = null;
  479. try {
  480. URL url = new URL("http://api.webcamapp.datatask.net/" +
  481. cameraHelperInstance.getCurrentCameraModel().getId());
  482. connection = (HttpURLConnection) url.openConnection();
  483. connection.connect();
  484. InputStream stream = connection.getInputStream();
  485. reader = new BufferedReader(new InputStreamReader(stream));
  486. StringBuilder buffer = new StringBuilder();
  487. String line;
  488. Log.d("GetValues", "MapIDCamURL: " + url);
  489.  
  490. while ((line = reader.readLine()) != null)
  491. buffer.append(line).append("\n");
  492. return buffer.toString();
  493.  
  494. } catch (MalformedURLException e) {
  495. e.printStackTrace();
  496.  
  497. } catch (IOException e) {
  498. e.printStackTrace();
  499.  
  500. } finally {
  501. if (connection != null) {
  502. connection.disconnect();
  503. }
  504. try {
  505. if (reader != null) {
  506. reader.close();
  507. }
  508. } catch (IOException e) {
  509. e.printStackTrace();
  510. }
  511. }
  512. return null;
  513. }
  514.  
  515. protected void onPostExecute(String result) {
  516. super.onPostExecute(result);
  517. JSONObject json_data;
  518. try {
  519. json_data = new JSONObject(result);
  520. for(int i = 0; i < json_data.length(); i++) {
  521. Log.d("HelloWebService", "Size: " + result);
  522.  
  523. JSONObject counters = json_data.getJSONObject("counters");
  524. long likes = counters.getInt("likes");
  525. likeProgressBar.setVisibility(View.GONE);
  526. likeTxtTv.setVisibility(View.VISIBLE);
  527.  
  528. if(likes < 0)
  529. likes = 0;
  530.  
  531. likeTxtTv.setText(Long.toString(likes));
  532. cameraHelperInstance.getCurrentCameraModel().setLikes(likes);
  533. break;
  534. }
  535. } catch (Exception e){
  536. e.printStackTrace();
  537. Log.d("CatchLikeException","Exc: " + e);
  538. }
  539. }
  540. }
  541. CameraInfoAsyncTask cameraInfoAsyncTask = new CameraInfoAsyncTask();
  542. cameraInfoAsyncTask.execute();
  543. }
  544.  
  545. private void addWarn(){
  546. isWarned = true;
  547. interstitialHelper.checkInterstitialCounter();
  548. warnFab.setImageResource(R.drawable.icon_iswarned);
  549. warnFab.setLabelText(getString(R.string.cam_is_warned));
  550. cameraDatabaseHelper.addWarn(cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
  551. isWarned = true;
  552. WebService webService = new WebService();
  553. webService.postWarnServer(cameraHelperInstance.getCurrentCameraModel().getId());
  554. }
  555.  
  556. public class Picture extends AsyncTask<String, File, File> {
  557. Bitmap image;
  558. File file = null;
  559. protected void onPreExecute() {
  560. super.onPreExecute();
  561. pd = new ProgressDialog(CameraActivity.this);
  562. pd.setMessage(getResources().getString(R.string.cameras_screen_shot_is_saving));
  563. pd.setCancelable(false);
  564. pd.show();
  565. }
  566.  
  567. protected File doInBackground(String... params) {
  568. String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
  569. String fileName = "Screen" + timeStamp + "_" +
  570. cameraHelperInstance.getCurrentCameraModel().getCity() + ".JPG";
  571. if(isMJPG) {
  572. image = lastPreview;
  573. } else {
  574. try {
  575. if (imageView != null && imageViewSecond != null) {
  576. if (activeCameraImageView) {
  577. imageView.buildDrawingCache();
  578. image = imageView.getDrawingCache();
  579. } else {
  580. imageViewSecond.buildDrawingCache();
  581. image = imageViewSecond.getDrawingCache();
  582. }
  583. }
  584. } catch (NullPointerException e) {
  585. e.printStackTrace();
  586. }
  587. }
  588. try {
  589. file = new File(new File(Environment.getExternalStorageDirectory() + File.separator + "WebCamScreenShots"), fileName);
  590. FileOutputStream out = new FileOutputStream(file);
  591. if(image != null)
  592. image.compress(Bitmap.CompressFormat.JPEG, 100, out);
  593. out.flush();
  594. out.close();
  595. } catch (IOException e) {
  596. e.printStackTrace();
  597. } catch (NullPointerException e) {
  598. e.printStackTrace();
  599. } /*finally {
  600. if(file != null)
  601. shareScreenShot(file);
  602. }*/
  603. return file;
  604. }
  605.  
  606. @Override
  607. protected void onPostExecute(File result) {
  608. super.onPostExecute(result);
  609. if(pd != null) {
  610. pd.cancel();
  611. }
  612. if(file != null)
  613. shareScreenShot(result);
  614. }
  615. }
  616.  
  617. private void checkCameraType() {
  618. CameraTypeGenerator cameraTypeGenerator = new CameraTypeGenerator(
  619. cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
  620. if (cameraTypeGenerator.getCameraType() != null) {
  621. if (cameraTypeGenerator.getCameraType().toString().equals(CameraType.MJPG.toString())) {
  622. initMJPGFormatSmall();
  623. type = CameraType.MJPEG.toString();
  624. } else {
  625. initJPEGFormatSmall();
  626. type = CameraType.JPG.toString();
  627. }
  628. }
  629. }
  630.  
  631. private void initMJPGFormatSmall(){
  632. isMJPG = true;
  633. MJPGLayout.setVisibility(View.VISIBLE);
  634. imageView.setVisibility(View.GONE);
  635. imageViewSecond.setVisibility(View.GONE);
  636. loadIpCam();
  637. }
  638.  
  639. private void initJPEGFormatSmall(){
  640. Log.d("GetUrlToCheckJPEG", "Url: " + cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
  641. imagesLayout.setVisibility(View.VISIBLE);
  642. MJPGLayout.setVisibility(View.GONE);
  643. imageView.setVisibility(View.VISIBLE);
  644. floatingActionButton.setVisibility(View.VISIBLE);
  645. imageViewSecond.setVisibility(View.GONE);
  646. checkIfIsLoaded();
  647. }
  648.  
  649. public void initToolbar(){
  650. toolbar = (Toolbar) findViewById(R.id.toolbar);
  651. setSupportActionBar(toolbar);
  652. ActionBar actionBar = getActionBar();
  653. if(actionBar != null){
  654. actionBar.setTitle(getString(R.string.camera_activity_name));
  655. actionBar.setDisplayHomeAsUpEnabled(true);
  656. actionBar.setDisplayShowHomeEnabled(true);
  657. }
  658. }
  659.  
  660. private void createFolder(){
  661. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
  662. if (ContextCompat.checkSelfPermission(this,
  663. Manifest.permission.WRITE_EXTERNAL_STORAGE)
  664. == PackageManager.PERMISSION_GRANTED) {
  665. fileTool.createFolder();
  666. }
  667. int MY_MULTIPLE_PERMISSION = 1;
  668. fileTool.checkPermissions2(this, MY_MULTIPLE_PERMISSION);
  669. } else {
  670. fileTool.createFolder();
  671. }
  672. }
  673.  
  674. public void initWidgets() {
  675. cameraAdapter = new CameraAdapter();
  676. likeProgressBar = (ProgressBar) findViewById(R.id.progressBarLoadingLikesCameras);
  677. likeProgressBar.setVisibility(View.VISIBLE);
  678.  
  679. fileTool = new FileTool();
  680. imagesLayout = (LinearLayout) findViewById(R.id.images);
  681. imagesLayout.setVisibility(View.VISIBLE);
  682.  
  683. MJPGLayout = (LinearLayout) findViewById(R.id.mjpglayout);
  684. MJPEGViewSmall = (MjpegView) findViewById(R.id.mjpegViewDefault);
  685.  
  686. MJPEGViewSmall.setCustomBackgroundColor(Color.DKGRAY);
  687. MJPEGViewSmall.getSurfaceView().setZOrderOnTop(false);
  688. MJPEGViewSmall.setOnFrameCapturedListener(this);
  689.  
  690. /* Button fullScreenBt = (Button) findViewById(R.id.fullscreen);
  691. Button historyBt = (Button) findViewById(R.id.history);*/
  692. /*
  693. snapshotBt = (Button) findViewById(R.id.snapshot);
  694. */
  695. /*
  696. snapshotBt.setBackground(getResources().getDrawable(R.drawable.icon_snapshot));
  697. */
  698.  
  699. /*snapshotBt.setOnClickListener(this);*/
  700. /*fullScreenBt.setOnClickListener(this);
  701. historyBt.setOnClickListener(this);
  702. */
  703. RelativeLayout mainRelativeLayout = (RelativeLayout) findViewById(R.id.main_relative_layout);
  704.  
  705. MJPEGViewSmall.setCustomBackgroundColor(R.color.blackMainActivity);
  706.  
  707. imageView = (ImageView) findViewById(R.id.videoView);
  708. imageViewSecond = (ImageView) findViewById(R.id.videoViewSecond);
  709.  
  710. imageNoCam = (ImageView) findViewById(R.id.no_cam);
  711. imageNoCam.setVisibility(View.GONE);
  712.  
  713. TextView regionTv = (TextView) findViewById(R.id.regionTv);
  714. TextView countryTv = (TextView) findViewById(R.id.countryTv);
  715. categoryTv = (TextView) findViewById(R.id.categoryValue);
  716. categoryTxtTv = (TextView) findViewById(R.id.categoryText);
  717. likeTxtTv = (TextView) findViewById(R.id.likeText);
  718. categoryTxtTv.setText(getResources().getString(R.string.category_txt_1));
  719.  
  720. floatingActionButton = (com.github.clans.fab.FloatingActionMenu) findViewById(R.id.location_fab);
  721. floatingActionButton.setOnClickListener(this);
  722.  
  723. ViewTreeObserver vto = floatingActionButton.getViewTreeObserver();
  724. vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
  725. @Override
  726. public void onGlobalLayout() {
  727. floatingActionButton.getMenuIconView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
  728. int width = floatingActionButton.getChildAt(0).getMeasuredWidth();
  729. int height = floatingActionButton.getChildAt(0).getMeasuredHeight();
  730.  
  731. RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)
  732. floatingActionButton.getLayoutParams());
  733. params.bottomMargin = -(height / 2);
  734.  
  735. floatingActionButton.setLayoutParams(params);
  736.  
  737. Log.d("HelloMargin","Width: " + width);
  738. Log.d("HelloMargin","Height: " + height);
  739.  
  740. }
  741. });
  742.  
  743. addToFavFAB = (com.github.clans.fab.FloatingActionButton) findViewById(R.id.add_to_fav_fab);
  744. addToFavFAB.setOnClickListener(this);
  745. warnFab = (com.github.clans.fab.FloatingActionButton) findViewById(R.id.warn_fab);
  746. warnFab.setOnClickListener(this);
  747.  
  748. likeTb = (ToggleButton) findViewById(R.id.like);
  749. likeTb.setOnClickListener(this);
  750. likeTb.setBackgroundDrawable(getResources().getDrawable(R.drawable.ico_like_off));
  751.  
  752. Intent intent = getIntent();
  753. url = intent.getStringExtra(KEY_URL);
  754.  
  755. /*
  756. url = "http://85.129.34.79:81/snapshot.cgi?loginuse=admin&loginpas=";
  757. */
  758.  
  759. category = intent.getStringExtra(KEY_CATEGORY);
  760. region = intent.getStringExtra(KEY_REGION);
  761. country = intent.getStringExtra(KEY_COUNTRY);
  762. city = intent.getStringExtra(KEY_CITY);
  763.  
  764. toolbar.setTitle(city);
  765. regionTv.setText(region);
  766. countryTv.setText(country);
  767.  
  768. if(category != null) {
  769. if (category.length() > 0) {
  770. category = category.toLowerCase();
  771. category = category.substring(0, 1).toUpperCase() + category.substring(1);
  772. }
  773. }
  774. categoryTv.setText(category);
  775. likeTxtTv.setVisibility(View.GONE);
  776.  
  777. checkValueOfCategory();
  778.  
  779. mainRelativeLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
  780. floatingActionButton.setVisibility(View.GONE);
  781.  
  782. for (CameraModel CameraModelLike : likeList){
  783. try {
  784. if (CameraModelLike.getCameraUrl().equals(url)) {
  785. likeTb.setBackgroundDrawable(getResources().getDrawable(R.drawable.ico_like_on));
  786. isLiked = true;
  787. }
  788. } catch (Exception e) {
  789. e.printStackTrace();
  790. }
  791. }
  792. for(CameraModel cameraModelWarns : warnList){
  793. if(cameraModelWarns.getCameraUrl().equals(url)) {
  794. warnFab.setImageResource(R.drawable.icon_iswarned);
  795. warnFab.setLabelText(getString(R.string.cam_is_warned));
  796. isWarned = true;
  797. }
  798. }
  799.  
  800. for (CameraModel cameraModelFav : cameraListFavDB) {
  801. if (cameraModelFav.getCameraUrl().equals(cameraHelperInstance.getCurrentCameraModel().getCameraUrl())) {
  802. addToFavFAB.setImageResource(R.drawable.ico_is_favourite);
  803. addToFavFAB.setLabelText(getString(R.string.remove_from_db));
  804. isFavourite = true;
  805. }
  806. }
  807.  
  808. firstButton = (LinearLayout) findViewById(R.id.first);
  809. secondButton = (LinearLayout) findViewById(R.id.second);
  810. LinearLayout thirdButton = (LinearLayout) findViewById(R.id.third);
  811. LinearLayout fourthButton = (LinearLayout) findViewById(R.id.four);
  812.  
  813. firstButton.setOnClickListener(this);
  814. secondButton.setOnClickListener(this);
  815. thirdButton.setOnClickListener(this);
  816. fourthButton.setOnClickListener(this);
  817.  
  818. progressBarCam = (CircularProgressView) findViewById(R.id.determinate_Bar_cameras);
  819. progressBarCam.setVisibility(View.VISIBLE);
  820. imagesLayout.setVisibility(View.GONE);
  821. cameraHelperInstance.getCurrentCameraModel().setCameraUrl(url);
  822. /*
  823. checkCameraType();
  824. */
  825. addCameraToHistory();
  826. }
  827.  
  828. private void addCameraToHistory() {
  829. class DatabaseHistory extends AsyncTask<String, String, String> {
  830. protected void onPreExecute() {
  831. super.onPreExecute();
  832. }
  833. protected String doInBackground(String... params) {
  834. ArrayList<CameraModel> list = cameraDatabaseHelper.allVideosHistory();
  835. boolean saveToDatabase = true;
  836. for (CameraModel cameraModel : list) {
  837. Log.d("FirstVal","CameraModel1 " + cameraModel.getId());
  838. Log.d("FirstVal","CameraModelHelper " + cameraHelperInstance.getCurrentCameraModel().getId());
  839. if(cameraModel.getId() == cameraHelperInstance.getCurrentCameraModel().getId()) {
  840. saveToDatabase = false;
  841. }
  842. }
  843.  
  844. if(saveToDatabase) {
  845. cameraDatabaseHelper.addCameraModelHistory(cameraHelperInstance.getCurrentCameraModel());
  846. } else {
  847. for (CameraModel cameraModel : list){
  848. if(cameraModel.getId() == cameraHelperInstance.getCurrentCameraModel().getId()){
  849. cameraDatabaseHelper.removeFromDatabaseHistory(
  850. cameraHelperInstance.getCurrentCameraModel(), getApplicationContext());
  851. cameraDatabaseHelper.addCameraModelHistory(cameraHelperInstance.getCurrentCameraModel());
  852. }
  853. }
  854. }
  855. return null;
  856. }
  857.  
  858. @Override
  859. protected void onPostExecute(String result) {
  860. super.onPostExecute(result);
  861. }
  862. }
  863. DatabaseHistory databaseHistory = new DatabaseHistory();
  864. databaseHistory.execute();
  865. }
  866.  
  867. private void addToFavDb(){
  868. cameraDatabaseHelper.addVideoModelFavourites(
  869. cameraHelperInstance.getCurrentCameraModel());
  870. addToFavFAB.setImageResource(R.drawable.ico_is_favourite);
  871. addToFavFAB.setLabelText(getString(R.string.remove_from_db));
  872. isFavourite = true;
  873. cameraAdapter.notifyDataSetChanged();
  874. }
  875.  
  876. private void removeFromDB(){
  877. cameraDatabaseHelper.removeFromFavourites(cameraHelperInstance.getCurrentCameraModel().getCameraUrl(), this);
  878. addToFavFAB.setImageResource(R.drawable.ico_star);
  879. addToFavFAB.setLabelText(getString(R.string.add_to_fav_desc));
  880. isFavourite = false;
  881. cameraAdapter.notifyDataSetChanged();
  882. }
  883.  
  884. private void loadIpCam() {
  885. floatingActionButton.setVisibility(View.VISIBLE);
  886. imagesLayout.setVisibility(View.INVISIBLE);
  887.  
  888. if(mjpeg == null) {
  889. Log.d("HelloMJPEG","InstanceisNull");
  890. mjpeg = Mjpeg.newInstance();
  891. }
  892.  
  893. class AllCamerasAsyncTask extends AsyncTask<String, String, String> {
  894. private boolean isError = false;
  895. @Override
  896. protected void onPreExecute() {
  897. super.onPreExecute();
  898. }
  899. @Override
  900. protected String doInBackground(String... params) {
  901. try {
  902. mjpeg.open(url, TIMEOUT)
  903. .subscribe(
  904. inputStream -> {
  905. MJPEGViewSmall.resetTransparentBackground();
  906. MJPEGViewSmall.setCustomBackgroundColor(
  907. getResources().getColor(R.color.colorPrimary));
  908. MJPEGViewSmall.setSource(inputStream);
  909. MJPEGViewSmall.setDisplayMode(DisplayMode.FULLSCREEN);
  910.  
  911. },
  912. throwable ->{
  913. isError = true;
  914. Log.e(getClass().getSimpleName(),
  915. "mjpeg error", throwable);
  916. });
  917.  
  918. } catch (NetworkOnMainThreadException e) {
  919. e.printStackTrace();
  920. isError = true;
  921. Log.e(getClass().getSimpleName(),
  922. "mjpeg error", e);
  923. } catch (NullPointerException e){
  924. e.printStackTrace();
  925. isError = true;
  926. Log.e(getClass().getSimpleName(),
  927. "mjpeg error", e);
  928. }
  929. return null;
  930. }
  931.  
  932. @Override
  933. protected void onPostExecute(String result) {
  934. super.onPostExecute(result);
  935. if(isError) {
  936. noCamSmaller();
  937. } else {
  938. showLayoutMJPEGView();
  939. isCameraActive = true;
  940. }
  941. }
  942. }
  943. AllCamerasAsyncTask allCamerasAsyncTask = new AllCamerasAsyncTask();
  944. allCamerasAsyncTask.execute();
  945. }
  946.  
  947. private void showLayoutMJPEGView(){
  948. progressBarCam.setVisibility(View.GONE);
  949. imagesLayout.setVisibility(View.VISIBLE);
  950. }
  951.  
  952. private void noCamSmaller(){
  953. Log.d("NoCameraMethodActive","True");
  954. imageNoCam.setVisibility(View.VISIBLE);
  955. imagesLayout.setVisibility(View.GONE);
  956. floatingActionButton.setVisibility(View.GONE);
  957. progressBarCam.setVisibility(View.GONE);
  958. MJPGLayout.setVisibility(View.GONE);
  959. }
  960.  
  961. private void doTheAutoRefresh() {
  962. isCameraActive = true;
  963. imageViewSecond.setScaleType(ImageView.ScaleType.FIT_XY);
  964. Picasso.with(this)
  965. .load(url)
  966. .memoryPolicy(MemoryPolicy.NO_CACHE)
  967. .noFade()
  968. .into(imageViewSecond, new Callback.EmptyCallback(){
  969. @Override
  970. public void onSuccess() {
  971. activeCameraImageView = false;
  972. imageView.setVisibility(View.GONE);
  973. imageViewSecond.setVisibility(View.VISIBLE);
  974. checkIfIsLoaded();
  975. }
  976. @Override
  977. public void onError() {
  978. noCamSmaller();
  979. Log.e("Error","Picasso");
  980. super.onError();
  981. }
  982. });
  983. }
  984.  
  985. private void checkIfIsLoaded() {
  986. imageView.setScaleType(ImageView.ScaleType.FIT_XY);
  987. Picasso.with(getApplicationContext())
  988. .load(url)
  989. .noFade()
  990. .fit()
  991. .memoryPolicy(MemoryPolicy.NO_CACHE)
  992. .into(imageView, new Callback.EmptyCallback() {
  993. @Override
  994. public void onSuccess() {
  995. activeCameraImageView = true;
  996. imageView.setVisibility(View.VISIBLE);
  997. imageViewSecond.setVisibility(View.GONE);
  998. progressBarCam.setVisibility(View.GONE);
  999. loaded.set(true);
  1000. if(!isFinished)
  1001. doTheAutoRefresh();
  1002. }
  1003.  
  1004. @Override
  1005. public void onError() {
  1006. noCamSmaller();
  1007. Log.e("Error","Picasso");
  1008. super.onError();
  1009. }
  1010. });
  1011. }
  1012.  
  1013. protected void advert(){
  1014. MobileAds.initialize(this, "ca-app-pub-4223710547139679/1150782804");
  1015. AdView mAdView = (AdView) findViewById(R.id.adView);
  1016. AdRequest adRequest = new AdRequest.Builder().build();
  1017. mAdView.loadAd(adRequest);
  1018. interstitialHelper = new InterstitialHelper(this, 10,0,
  1019. "ca-app-pub-4223710547139679/4487392589", getString(R.string.loading_add));
  1020. }
  1021.  
  1022. private void initHelpers(){
  1023. isFinished = false;
  1024. sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());
  1025. }
  1026.  
  1027. public Dialog onReportCamera(Context context) {
  1028. return new AlertDialog.Builder(context, R.style.AppCompatAlertDialogStyle)
  1029. .setTitle(context.getString(R.string.onReportCamera))
  1030. .setPositiveButton(context.getString(R.string.yes),
  1031. (dialog, whichButton) -> addWarn()
  1032. )
  1033. .setNegativeButton(context.getString(R.string.no),
  1034. (dialog, whichButton) -> dialog.dismiss()
  1035. )
  1036. .show();
  1037. }
  1038.  
  1039. private void shareScreenShot(File file) {
  1040. Intent shareIntent = new Intent();
  1041. shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.app_name));
  1042. shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(file.getAbsolutePath()))); //optional//use this when you want to send an image
  1043. shareIntent.setType("image/png");
  1044. shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  1045. startActivity(Intent.createChooser(shareIntent, getString(R.string.share_title) ));
  1046. }
  1047.  
  1048. private void initDatabase(){
  1049. cameraDatabaseHelper = new CameraDatabaseHelper(getApplicationContext());
  1050. cameraDatabaseHelper.getWritableDatabase();
  1051. likeList = cameraDatabaseHelper.allLikes();
  1052. warnList = cameraDatabaseHelper.allWarns();
  1053. cameraListFavDB = cameraDatabaseHelper.allFavCameras();
  1054. ArrayList<CameraModel> historyList = cameraDatabaseHelper.allVideosHistory();
  1055.  
  1056. if(historyList.size() > 40) {
  1057. cameraDatabaseHelper.removeAllFromTableHistory(getApplicationContext());
  1058. }
  1059. }
  1060. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement