Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package webcam.apps.android.freemium.com.webcam;
- import android.Manifest;
- import android.app.ActionBar;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.pm.ActivityInfo;
- import android.content.pm.PackageManager;
- import android.graphics.Bitmap;
- import android.graphics.Color;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.NetworkOnMainThreadException;
- import android.preference.PreferenceManager;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewTreeObserver;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.ProgressBar;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.ToggleButton;
- import com.github.niqdev.mjpeg.DisplayMode;
- import com.github.niqdev.mjpeg.Mjpeg;
- import com.github.niqdev.mjpeg.MjpegView;
- import com.github.niqdev.mjpeg.OnFrameCapturedListener;
- import com.github.rahatarmanahmed.cpv.CircularProgressView;
- import com.google.android.gms.ads.AdRequest;
- import com.google.android.gms.ads.AdView;
- import com.google.android.gms.ads.MobileAds;
- import com.squareup.picasso.Callback;
- import com.squareup.picasso.MemoryPolicy;
- import com.squareup.picasso.Picasso;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.concurrent.atomic.AtomicBoolean;
- import webcam.apps.android.freemium.com.webcam.Enum.CameraType;
- import webcam.apps.android.freemium.com.webcam.Utils.CameraModel;
- import webcam.apps.android.freemium.com.webcam.Utils.CameraTypeGenerator;
- import webcam.apps.android.freemium.com.webcam.Utils.FileTool;
- import webcam.apps.android.freemium.com.webcam.Utils.InterstitialHelper;
- import webcam.apps.android.freemium.com.webcam.Utils.CameraAdapter;
- import webcam.apps.android.freemium.com.webcam.Utils.CameraDatabaseHelper;
- import webcam.apps.android.freemium.com.webcam.Utils.CameraHelper;
- import webcam.apps.android.freemium.com.webcam.Utils.WebService;
- /**
- * Created by tymoteusz on 05.08.17.
- */
- public class CameraActivity extends AppCompatActivity implements View.OnClickListener,
- OnFrameCapturedListener {
- private final AtomicBoolean loaded = new AtomicBoolean();
- private final int TIMEOUT = 5;
- private final int MEDIA_PERMISSION_REQUEST_CODE = 100;
- private final String KEY_URL = "url";
- private final String KEY_COUNTRY = "country";
- private final String KEY_REGION = "region";
- private final String KEY_CATEGORY = "category";
- private final String KEY_CITY = "city";
- private final String URL_TYPE_GENERATOR = "urlTypeGenerator";
- private final String KEY_CLASS_NAME = "className";
- private String url = "";
- private String category = "";
- private String region = "";
- private String city = "";
- private String country = "";
- private String type;
- private ImageView imageView;
- private ImageView imageViewSecond;
- private ImageView imageNoCam;
- private InterstitialHelper interstitialHelper;
- private Toolbar toolbar;
- private MjpegView MJPEGViewSmall;
- private LinearLayout MJPGLayout;
- private LinearLayout imagesLayout;
- private LinearLayout secondButton;
- private LinearLayout firstButton;
- private boolean isFinished;
- private boolean isFavourite = false;
- private boolean isWarned = false;
- private boolean isLiked = false;
- private boolean isCameraActive = false;
- private boolean isMJPG = false;
- private boolean activeCameraImageView;
- private CameraHelper cameraHelperInstance = CameraHelper.getInstance();
- private CameraDatabaseHelper cameraDatabaseHelper;
- private ArrayList<CameraModel> cameraListFavDB;
- private CameraAdapter cameraAdapter;
- private com.github.clans.fab.FloatingActionButton addToFavFAB;
- private com.github.clans.fab.FloatingActionButton warnFab;
- private com.github.clans.fab.FloatingActionMenu floatingActionButton;
- public SharedPreferences sharedPreferences;
- private ToggleButton likeTb;
- private TextView categoryTv;
- private TextView categoryTxtTv;
- private TextView likeTxtTv;
- public FileTool fileTool;
- private CircularProgressView progressBarCam;
- private ProgressDialog pd;
- private ProgressBar likeProgressBar;
- private ArrayList<CameraModel> likeList;
- private ArrayList<CameraModel> warnList;
- private Bitmap lastPreview = null;
- private Mjpeg mjpeg;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.camera_layout);
- initDatabase();
- setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
- advert();
- initToolbar();
- initWidgets();
- initHelpers();
- addView();
- getLikesFromServer();
- createFolder();
- checkCameraType();
- }
- private void checkValueOfCategory(){
- if(category != null) {
- if (category.contains("None") || category.contains("")) {
- categoryTv.setVisibility(View.GONE);
- categoryTxtTv.setVisibility(View.GONE);
- }
- } else {
- categoryTv.setVisibility(View.GONE);
- categoryTxtTv.setVisibility(View.GONE);
- }
- }
- @Override
- public void onFrameCaptured(Bitmap bitmap) {
- lastPreview = bitmap;
- }
- private void cleanActivityMJPG(){
- class MjpgClean extends AsyncTask<String, String, String> {
- protected void onPreExecute() {
- super.onPreExecute();
- }
- protected String doInBackground(String... params) {
- try {
- MJPEGViewSmall.stopPlayback();
- } catch (NullPointerException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- }
- }
- MjpgClean mjpgClean = new MjpgClean();
- mjpgClean.execute();
- }
- @Override
- protected void onPause() {
- super.onPause();
- try {
- if(MJPEGViewSmall.isStreaming()) {
- Thread thread = new Thread(() -> {
- try {
- MJPEGViewSmall.stopPlayback();
- } catch (Exception e) {
- e.printStackTrace();
- }
- });
- thread.start();
- }
- } catch (NullPointerException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected void onDestroy() {
- isFinished = true;
- super.onDestroy();
- }
- @Override
- protected void onResume() {
- super.onResume();
- checkCameraType();
- firstButton.setEnabled(true);
- }
- public void setSupportActionBar(Toolbar toolbar) {
- getDelegate().setSupportActionBar(toolbar);
- toolbar.setNavigationIcon(R.drawable.ico_left);
- toolbar.setTitle(R.string.camera_activity_name);
- toolbar.setNavigationOnClickListener(view -> {
- onBackPressed();
- });
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- }
- @Override
- protected void onStop() {
- super.onStop();
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
- @NonNull int[] grantResults) {
- switch (requestCode) {
- case MEDIA_PERMISSION_REQUEST_CODE:
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- Picture picture = new Picture();
- picture.execute();
- }
- break;
- }
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.add_to_fav_fab:
- interstitialHelper.checkInterstitialCounter();
- if (!isFavourite) {
- addToFavDb();
- isFavourite = true;
- } else {
- removeFromDB();
- isFavourite = false;
- }
- break;
- case R.id.warn_fab:
- interstitialHelper.checkInterstitialCounter();
- if (!isWarned) {
- onReportCamera(this);
- }
- break;
- case R.id.snapshot:
- if(isCameraActive){
- if (ActivityCompat.checkSelfPermission(CameraActivity.this,
- Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(CameraActivity.this,
- new String[]{
- Manifest.permission.WRITE_EXTERNAL_STORAGE,
- Manifest.permission.WRITE_EXTERNAL_STORAGE},
- MEDIA_PERMISSION_REQUEST_CODE);
- } else {
- Picture picture = new Picture();
- Log.d("GetCurrentImage","Image: " + cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
- if(MJPEGViewSmall != null || imageView != null || imageViewSecond != null) {
- picture.execute();
- firstButton.setEnabled(false);
- } else {
- CharSequence txt = getString(R.string.error_while_saving);
- Toast toast = Toast.makeText(this, txt, Toast.LENGTH_SHORT);
- toast.show();
- }
- }
- } else {
- CharSequence txt = getString(R.string.error_while_saving);
- Toast toast = Toast.makeText(this, txt, Toast.LENGTH_SHORT);
- toast.show();
- }
- break;
- case R.id.history:
- goToHistory();
- break;
- case R.id.fullscreen:
- goToFullScreen();
- break;
- case R.id.first:
- if(isCameraActive){
- if (ActivityCompat.checkSelfPermission(CameraActivity.this,
- Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(CameraActivity.this,
- new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
- Manifest.permission.WRITE_EXTERNAL_STORAGE},
- MEDIA_PERMISSION_REQUEST_CODE);
- } else {
- firstButton.setEnabled(false);
- Picture picture = new Picture();
- Log.d("GetCurrentImage","Image: " + cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
- if(MJPEGViewSmall != null || imageView != null || imageViewSecond != null) {
- picture.execute();
- firstButton.setEnabled(false);
- } else {
- CharSequence txt = getString(R.string.error_while_saving);
- Toast toast = Toast.makeText(this, txt, Toast.LENGTH_SHORT);
- toast.show();
- firstButton.setEnabled(true);
- }
- }
- } else {
- CharSequence txt = getString(R.string.error_while_saving);
- Toast toast = Toast.makeText(this, txt, Toast.LENGTH_SHORT);
- toast.show();
- }
- break;
- case R.id.second:
- goToHistory();
- break;
- case R.id.third:
- goToFullScreen();
- break;
- case R.id.like:
- interstitialHelper.checkInterstitialCounter();
- if (!isLiked) {
- addLike();
- isLiked = true;
- } else {
- removeLike();
- isLiked = false;
- }
- break;
- }
- }
- private void goToHistory() {
- class HistoryClean extends AsyncTask<String, String, String> {
- protected void onPreExecute() {
- super.onPreExecute();
- secondButton.setEnabled(false);
- }
- protected String doInBackground(String... params) {
- try {
- if (MJPEGViewSmall.isStreaming()) {
- MJPEGViewSmall.stopPlayback();
- }
- } catch(Exception e){
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- Intent intentFullScreen = new Intent(CameraActivity.this, HistoryActivity.class);
- intentFullScreen.putExtra(KEY_URL, url);
- intentFullScreen.putExtra(KEY_URL, url);
- intentFullScreen.putExtra(KEY_CATEGORY, category);
- intentFullScreen.putExtra(KEY_REGION, region);
- intentFullScreen.putExtra(KEY_COUNTRY, country);
- intentFullScreen.putExtra(KEY_CITY, city);
- intentFullScreen.putExtra(KEY_CLASS_NAME, "camera");
- intentFullScreen.putExtra(URL_TYPE_GENERATOR, type);
- startActivity(intentFullScreen);
- secondButton.setEnabled(true);
- CameraActivity.this.onStop();
- }
- }
- HistoryClean historyClean = new HistoryClean();
- historyClean.execute();
- }
- private void goToFullScreen(){
- class MjpgClean extends AsyncTask<String, String, String> {
- protected void onPreExecute() {
- super.onPreExecute();
- }
- protected String doInBackground(String... params) {
- /*try {
- if (MJPEGViewSmall.isStreaming()) {
- MJPEGViewSmall.stopPlayback();
- }
- } catch(Exception e){
- e.printStackTrace();
- }*/
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- Intent intentFullScreen = new Intent(CameraActivity.this, FullScreenActivity.class);
- intentFullScreen.putExtra(KEY_URL, url);
- intentFullScreen.putExtra(KEY_CATEGORY, category);
- intentFullScreen.putExtra(KEY_REGION, region);
- intentFullScreen.putExtra(KEY_COUNTRY, country);
- intentFullScreen.putExtra(KEY_CITY, city);
- intentFullScreen.putExtra(URL_TYPE_GENERATOR, type);
- startActivity(intentFullScreen);
- }
- }
- MjpgClean mjpgClean = new MjpgClean();
- mjpgClean.execute();
- }
- private void addView(){
- WebService webService = new WebService();
- webService.postViewServer(cameraHelperInstance.getCurrentCameraModel().getId());
- }
- private void addLike(){
- likeTb.setBackgroundDrawable(getResources().getDrawable(R.drawable.ico_like_on));
- isLiked = true;
- likeTxtTv.setText(String.valueOf(Integer.parseInt(likeTxtTv.getText().toString()) + 1));
- cameraDatabaseHelper.addLike(cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
- WebService webService = new WebService();
- webService.postLikeServer(cameraHelperInstance.getCurrentCameraModel().getId());
- }
- private void removeLike(){
- interstitialHelper.checkInterstitialCounter();
- likeTb.setBackgroundDrawable(getResources().getDrawable(R.drawable.ico_like_off));
- isLiked = false;
- long s = Integer.parseInt(likeTxtTv.getText().toString()) - 1;
- if(s == -1)
- s = 0;
- likeTxtTv.setText(String.valueOf(s));
- cameraDatabaseHelper.removeFromLike(
- cameraHelperInstance.getCurrentCameraModel().getCameraUrl(), this);
- WebService webService = new WebService();
- webService.postRemoveLikeServer(cameraHelperInstance.getCurrentCameraModel().getId());
- }
- private void getLikesFromServer(){
- class CameraInfoAsyncTask extends AsyncTask<String, String, String> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- @Override
- protected String doInBackground(String... params) {
- HttpURLConnection connection = null;
- BufferedReader reader = null;
- try {
- URL url = new URL("http://api.webcamapp.datatask.net/" +
- cameraHelperInstance.getCurrentCameraModel().getId());
- connection = (HttpURLConnection) url.openConnection();
- connection.connect();
- InputStream stream = connection.getInputStream();
- reader = new BufferedReader(new InputStreamReader(stream));
- StringBuilder buffer = new StringBuilder();
- String line;
- Log.d("GetValues", "MapIDCamURL: " + url);
- while ((line = reader.readLine()) != null)
- buffer.append(line).append("\n");
- return buffer.toString();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (connection != null) {
- connection.disconnect();
- }
- try {
- if (reader != null) {
- reader.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- JSONObject json_data;
- try {
- json_data = new JSONObject(result);
- for(int i = 0; i < json_data.length(); i++) {
- Log.d("HelloWebService", "Size: " + result);
- JSONObject counters = json_data.getJSONObject("counters");
- long likes = counters.getInt("likes");
- likeProgressBar.setVisibility(View.GONE);
- likeTxtTv.setVisibility(View.VISIBLE);
- if(likes < 0)
- likes = 0;
- likeTxtTv.setText(Long.toString(likes));
- cameraHelperInstance.getCurrentCameraModel().setLikes(likes);
- break;
- }
- } catch (Exception e){
- e.printStackTrace();
- Log.d("CatchLikeException","Exc: " + e);
- }
- }
- }
- CameraInfoAsyncTask cameraInfoAsyncTask = new CameraInfoAsyncTask();
- cameraInfoAsyncTask.execute();
- }
- private void addWarn(){
- isWarned = true;
- interstitialHelper.checkInterstitialCounter();
- warnFab.setImageResource(R.drawable.icon_iswarned);
- warnFab.setLabelText(getString(R.string.cam_is_warned));
- cameraDatabaseHelper.addWarn(cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
- isWarned = true;
- WebService webService = new WebService();
- webService.postWarnServer(cameraHelperInstance.getCurrentCameraModel().getId());
- }
- public class Picture extends AsyncTask<String, File, File> {
- Bitmap image;
- File file = null;
- protected void onPreExecute() {
- super.onPreExecute();
- pd = new ProgressDialog(CameraActivity.this);
- pd.setMessage(getResources().getString(R.string.cameras_screen_shot_is_saving));
- pd.setCancelable(false);
- pd.show();
- }
- protected File doInBackground(String... params) {
- String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
- String fileName = "Screen" + timeStamp + "_" +
- cameraHelperInstance.getCurrentCameraModel().getCity() + ".JPG";
- if(isMJPG) {
- image = lastPreview;
- } else {
- try {
- if (imageView != null && imageViewSecond != null) {
- if (activeCameraImageView) {
- imageView.buildDrawingCache();
- image = imageView.getDrawingCache();
- } else {
- imageViewSecond.buildDrawingCache();
- image = imageViewSecond.getDrawingCache();
- }
- }
- } catch (NullPointerException e) {
- e.printStackTrace();
- }
- }
- try {
- file = new File(new File(Environment.getExternalStorageDirectory() + File.separator + "WebCamScreenShots"), fileName);
- FileOutputStream out = new FileOutputStream(file);
- if(image != null)
- image.compress(Bitmap.CompressFormat.JPEG, 100, out);
- out.flush();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (NullPointerException e) {
- e.printStackTrace();
- } /*finally {
- if(file != null)
- shareScreenShot(file);
- }*/
- return file;
- }
- @Override
- protected void onPostExecute(File result) {
- super.onPostExecute(result);
- if(pd != null) {
- pd.cancel();
- }
- if(file != null)
- shareScreenShot(result);
- }
- }
- private void checkCameraType() {
- CameraTypeGenerator cameraTypeGenerator = new CameraTypeGenerator(
- cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
- if (cameraTypeGenerator.getCameraType() != null) {
- if (cameraTypeGenerator.getCameraType().toString().equals(CameraType.MJPG.toString())) {
- initMJPGFormatSmall();
- type = CameraType.MJPEG.toString();
- } else {
- initJPEGFormatSmall();
- type = CameraType.JPG.toString();
- }
- }
- }
- private void initMJPGFormatSmall(){
- isMJPG = true;
- MJPGLayout.setVisibility(View.VISIBLE);
- imageView.setVisibility(View.GONE);
- imageViewSecond.setVisibility(View.GONE);
- loadIpCam();
- }
- private void initJPEGFormatSmall(){
- Log.d("GetUrlToCheckJPEG", "Url: " + cameraHelperInstance.getCurrentCameraModel().getCameraUrl());
- imagesLayout.setVisibility(View.VISIBLE);
- MJPGLayout.setVisibility(View.GONE);
- imageView.setVisibility(View.VISIBLE);
- floatingActionButton.setVisibility(View.VISIBLE);
- imageViewSecond.setVisibility(View.GONE);
- checkIfIsLoaded();
- }
- public void initToolbar(){
- toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- ActionBar actionBar = getActionBar();
- if(actionBar != null){
- actionBar.setTitle(getString(R.string.camera_activity_name));
- actionBar.setDisplayHomeAsUpEnabled(true);
- actionBar.setDisplayShowHomeEnabled(true);
- }
- }
- private void createFolder(){
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
- if (ContextCompat.checkSelfPermission(this,
- Manifest.permission.WRITE_EXTERNAL_STORAGE)
- == PackageManager.PERMISSION_GRANTED) {
- fileTool.createFolder();
- }
- int MY_MULTIPLE_PERMISSION = 1;
- fileTool.checkPermissions2(this, MY_MULTIPLE_PERMISSION);
- } else {
- fileTool.createFolder();
- }
- }
- public void initWidgets() {
- cameraAdapter = new CameraAdapter();
- likeProgressBar = (ProgressBar) findViewById(R.id.progressBarLoadingLikesCameras);
- likeProgressBar.setVisibility(View.VISIBLE);
- fileTool = new FileTool();
- imagesLayout = (LinearLayout) findViewById(R.id.images);
- imagesLayout.setVisibility(View.VISIBLE);
- MJPGLayout = (LinearLayout) findViewById(R.id.mjpglayout);
- MJPEGViewSmall = (MjpegView) findViewById(R.id.mjpegViewDefault);
- MJPEGViewSmall.setCustomBackgroundColor(Color.DKGRAY);
- MJPEGViewSmall.getSurfaceView().setZOrderOnTop(false);
- MJPEGViewSmall.setOnFrameCapturedListener(this);
- /* Button fullScreenBt = (Button) findViewById(R.id.fullscreen);
- Button historyBt = (Button) findViewById(R.id.history);*/
- /*
- snapshotBt = (Button) findViewById(R.id.snapshot);
- */
- /*
- snapshotBt.setBackground(getResources().getDrawable(R.drawable.icon_snapshot));
- */
- /*snapshotBt.setOnClickListener(this);*/
- /*fullScreenBt.setOnClickListener(this);
- historyBt.setOnClickListener(this);
- */
- RelativeLayout mainRelativeLayout = (RelativeLayout) findViewById(R.id.main_relative_layout);
- MJPEGViewSmall.setCustomBackgroundColor(R.color.blackMainActivity);
- imageView = (ImageView) findViewById(R.id.videoView);
- imageViewSecond = (ImageView) findViewById(R.id.videoViewSecond);
- imageNoCam = (ImageView) findViewById(R.id.no_cam);
- imageNoCam.setVisibility(View.GONE);
- TextView regionTv = (TextView) findViewById(R.id.regionTv);
- TextView countryTv = (TextView) findViewById(R.id.countryTv);
- categoryTv = (TextView) findViewById(R.id.categoryValue);
- categoryTxtTv = (TextView) findViewById(R.id.categoryText);
- likeTxtTv = (TextView) findViewById(R.id.likeText);
- categoryTxtTv.setText(getResources().getString(R.string.category_txt_1));
- floatingActionButton = (com.github.clans.fab.FloatingActionMenu) findViewById(R.id.location_fab);
- floatingActionButton.setOnClickListener(this);
- ViewTreeObserver vto = floatingActionButton.getViewTreeObserver();
- vto.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() {
- @Override
- public void onGlobalLayout() {
- floatingActionButton.getMenuIconView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
- int width = floatingActionButton.getChildAt(0).getMeasuredWidth();
- int height = floatingActionButton.getChildAt(0).getMeasuredHeight();
- RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)
- floatingActionButton.getLayoutParams());
- params.bottomMargin = -(height / 2);
- floatingActionButton.setLayoutParams(params);
- Log.d("HelloMargin","Width: " + width);
- Log.d("HelloMargin","Height: " + height);
- }
- });
- addToFavFAB = (com.github.clans.fab.FloatingActionButton) findViewById(R.id.add_to_fav_fab);
- addToFavFAB.setOnClickListener(this);
- warnFab = (com.github.clans.fab.FloatingActionButton) findViewById(R.id.warn_fab);
- warnFab.setOnClickListener(this);
- likeTb = (ToggleButton) findViewById(R.id.like);
- likeTb.setOnClickListener(this);
- likeTb.setBackgroundDrawable(getResources().getDrawable(R.drawable.ico_like_off));
- Intent intent = getIntent();
- url = intent.getStringExtra(KEY_URL);
- /*
- url = "http://85.129.34.79:81/snapshot.cgi?loginuse=admin&loginpas=";
- */
- category = intent.getStringExtra(KEY_CATEGORY);
- region = intent.getStringExtra(KEY_REGION);
- country = intent.getStringExtra(KEY_COUNTRY);
- city = intent.getStringExtra(KEY_CITY);
- toolbar.setTitle(city);
- regionTv.setText(region);
- countryTv.setText(country);
- if(category != null) {
- if (category.length() > 0) {
- category = category.toLowerCase();
- category = category.substring(0, 1).toUpperCase() + category.substring(1);
- }
- }
- categoryTv.setText(category);
- likeTxtTv.setVisibility(View.GONE);
- checkValueOfCategory();
- mainRelativeLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
- floatingActionButton.setVisibility(View.GONE);
- for (CameraModel CameraModelLike : likeList){
- try {
- if (CameraModelLike.getCameraUrl().equals(url)) {
- likeTb.setBackgroundDrawable(getResources().getDrawable(R.drawable.ico_like_on));
- isLiked = true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- for(CameraModel cameraModelWarns : warnList){
- if(cameraModelWarns.getCameraUrl().equals(url)) {
- warnFab.setImageResource(R.drawable.icon_iswarned);
- warnFab.setLabelText(getString(R.string.cam_is_warned));
- isWarned = true;
- }
- }
- for (CameraModel cameraModelFav : cameraListFavDB) {
- if (cameraModelFav.getCameraUrl().equals(cameraHelperInstance.getCurrentCameraModel().getCameraUrl())) {
- addToFavFAB.setImageResource(R.drawable.ico_is_favourite);
- addToFavFAB.setLabelText(getString(R.string.remove_from_db));
- isFavourite = true;
- }
- }
- firstButton = (LinearLayout) findViewById(R.id.first);
- secondButton = (LinearLayout) findViewById(R.id.second);
- LinearLayout thirdButton = (LinearLayout) findViewById(R.id.third);
- LinearLayout fourthButton = (LinearLayout) findViewById(R.id.four);
- firstButton.setOnClickListener(this);
- secondButton.setOnClickListener(this);
- thirdButton.setOnClickListener(this);
- fourthButton.setOnClickListener(this);
- progressBarCam = (CircularProgressView) findViewById(R.id.determinate_Bar_cameras);
- progressBarCam.setVisibility(View.VISIBLE);
- imagesLayout.setVisibility(View.GONE);
- cameraHelperInstance.getCurrentCameraModel().setCameraUrl(url);
- /*
- checkCameraType();
- */
- addCameraToHistory();
- }
- private void addCameraToHistory() {
- class DatabaseHistory extends AsyncTask<String, String, String> {
- protected void onPreExecute() {
- super.onPreExecute();
- }
- protected String doInBackground(String... params) {
- ArrayList<CameraModel> list = cameraDatabaseHelper.allVideosHistory();
- boolean saveToDatabase = true;
- for (CameraModel cameraModel : list) {
- Log.d("FirstVal","CameraModel1 " + cameraModel.getId());
- Log.d("FirstVal","CameraModelHelper " + cameraHelperInstance.getCurrentCameraModel().getId());
- if(cameraModel.getId() == cameraHelperInstance.getCurrentCameraModel().getId()) {
- saveToDatabase = false;
- }
- }
- if(saveToDatabase) {
- cameraDatabaseHelper.addCameraModelHistory(cameraHelperInstance.getCurrentCameraModel());
- } else {
- for (CameraModel cameraModel : list){
- if(cameraModel.getId() == cameraHelperInstance.getCurrentCameraModel().getId()){
- cameraDatabaseHelper.removeFromDatabaseHistory(
- cameraHelperInstance.getCurrentCameraModel(), getApplicationContext());
- cameraDatabaseHelper.addCameraModelHistory(cameraHelperInstance.getCurrentCameraModel());
- }
- }
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- }
- }
- DatabaseHistory databaseHistory = new DatabaseHistory();
- databaseHistory.execute();
- }
- private void addToFavDb(){
- cameraDatabaseHelper.addVideoModelFavourites(
- cameraHelperInstance.getCurrentCameraModel());
- addToFavFAB.setImageResource(R.drawable.ico_is_favourite);
- addToFavFAB.setLabelText(getString(R.string.remove_from_db));
- isFavourite = true;
- cameraAdapter.notifyDataSetChanged();
- }
- private void removeFromDB(){
- cameraDatabaseHelper.removeFromFavourites(cameraHelperInstance.getCurrentCameraModel().getCameraUrl(), this);
- addToFavFAB.setImageResource(R.drawable.ico_star);
- addToFavFAB.setLabelText(getString(R.string.add_to_fav_desc));
- isFavourite = false;
- cameraAdapter.notifyDataSetChanged();
- }
- private void loadIpCam() {
- floatingActionButton.setVisibility(View.VISIBLE);
- imagesLayout.setVisibility(View.INVISIBLE);
- if(mjpeg == null) {
- Log.d("HelloMJPEG","InstanceisNull");
- mjpeg = Mjpeg.newInstance();
- }
- class AllCamerasAsyncTask extends AsyncTask<String, String, String> {
- private boolean isError = false;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- @Override
- protected String doInBackground(String... params) {
- try {
- mjpeg.open(url, TIMEOUT)
- .subscribe(
- inputStream -> {
- MJPEGViewSmall.resetTransparentBackground();
- MJPEGViewSmall.setCustomBackgroundColor(
- getResources().getColor(R.color.colorPrimary));
- MJPEGViewSmall.setSource(inputStream);
- MJPEGViewSmall.setDisplayMode(DisplayMode.FULLSCREEN);
- },
- throwable ->{
- isError = true;
- Log.e(getClass().getSimpleName(),
- "mjpeg error", throwable);
- });
- } catch (NetworkOnMainThreadException e) {
- e.printStackTrace();
- isError = true;
- Log.e(getClass().getSimpleName(),
- "mjpeg error", e);
- } catch (NullPointerException e){
- e.printStackTrace();
- isError = true;
- Log.e(getClass().getSimpleName(),
- "mjpeg error", e);
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- if(isError) {
- noCamSmaller();
- } else {
- showLayoutMJPEGView();
- isCameraActive = true;
- }
- }
- }
- AllCamerasAsyncTask allCamerasAsyncTask = new AllCamerasAsyncTask();
- allCamerasAsyncTask.execute();
- }
- private void showLayoutMJPEGView(){
- progressBarCam.setVisibility(View.GONE);
- imagesLayout.setVisibility(View.VISIBLE);
- }
- private void noCamSmaller(){
- Log.d("NoCameraMethodActive","True");
- imageNoCam.setVisibility(View.VISIBLE);
- imagesLayout.setVisibility(View.GONE);
- floatingActionButton.setVisibility(View.GONE);
- progressBarCam.setVisibility(View.GONE);
- MJPGLayout.setVisibility(View.GONE);
- }
- private void doTheAutoRefresh() {
- isCameraActive = true;
- imageViewSecond.setScaleType(ImageView.ScaleType.FIT_XY);
- Picasso.with(this)
- .load(url)
- .memoryPolicy(MemoryPolicy.NO_CACHE)
- .noFade()
- .into(imageViewSecond, new Callback.EmptyCallback(){
- @Override
- public void onSuccess() {
- activeCameraImageView = false;
- imageView.setVisibility(View.GONE);
- imageViewSecond.setVisibility(View.VISIBLE);
- checkIfIsLoaded();
- }
- @Override
- public void onError() {
- noCamSmaller();
- Log.e("Error","Picasso");
- super.onError();
- }
- });
- }
- private void checkIfIsLoaded() {
- imageView.setScaleType(ImageView.ScaleType.FIT_XY);
- Picasso.with(getApplicationContext())
- .load(url)
- .noFade()
- .fit()
- .memoryPolicy(MemoryPolicy.NO_CACHE)
- .into(imageView, new Callback.EmptyCallback() {
- @Override
- public void onSuccess() {
- activeCameraImageView = true;
- imageView.setVisibility(View.VISIBLE);
- imageViewSecond.setVisibility(View.GONE);
- progressBarCam.setVisibility(View.GONE);
- loaded.set(true);
- if(!isFinished)
- doTheAutoRefresh();
- }
- @Override
- public void onError() {
- noCamSmaller();
- Log.e("Error","Picasso");
- super.onError();
- }
- });
- }
- protected void advert(){
- MobileAds.initialize(this, "ca-app-pub-4223710547139679/1150782804");
- AdView mAdView = (AdView) findViewById(R.id.adView);
- AdRequest adRequest = new AdRequest.Builder().build();
- mAdView.loadAd(adRequest);
- interstitialHelper = new InterstitialHelper(this, 10,0,
- "ca-app-pub-4223710547139679/4487392589", getString(R.string.loading_add));
- }
- private void initHelpers(){
- isFinished = false;
- sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());
- }
- public Dialog onReportCamera(Context context) {
- return new AlertDialog.Builder(context, R.style.AppCompatAlertDialogStyle)
- .setTitle(context.getString(R.string.onReportCamera))
- .setPositiveButton(context.getString(R.string.yes),
- (dialog, whichButton) -> addWarn()
- )
- .setNegativeButton(context.getString(R.string.no),
- (dialog, whichButton) -> dialog.dismiss()
- )
- .show();
- }
- private void shareScreenShot(File file) {
- Intent shareIntent = new Intent();
- shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.app_name));
- shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(file.getAbsolutePath()))); //optional//use this when you want to send an image
- shareIntent.setType("image/png");
- shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
- startActivity(Intent.createChooser(shareIntent, getString(R.string.share_title) ));
- }
- private void initDatabase(){
- cameraDatabaseHelper = new CameraDatabaseHelper(getApplicationContext());
- cameraDatabaseHelper.getWritableDatabase();
- likeList = cameraDatabaseHelper.allLikes();
- warnList = cameraDatabaseHelper.allWarns();
- cameraListFavDB = cameraDatabaseHelper.allFavCameras();
- ArrayList<CameraModel> historyList = cameraDatabaseHelper.allVideosHistory();
- if(historyList.size() > 40) {
- cameraDatabaseHelper.removeAllFromTableHistory(getApplicationContext());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement