Commit bfd31212 authored by Ishankha K.C's avatar Ishankha K.C

connect live feeds

parent c6a05d88
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.BabyCare" android:theme="@style/Theme.BabyCare"
tools:targetApi="31"> tools:targetApi="31">
<activity
android:name=".activities.LiveFeedActivity"
android:exported="false" />
<activity <activity
android:name=".activities.BabyDashboardActivity" android:name=".activities.BabyDashboardActivity"
android:exported="false" /> android:exported="false" />
......
...@@ -19,7 +19,6 @@ import androidx.appcompat.widget.Toolbar; ...@@ -19,7 +19,6 @@ import androidx.appcompat.widget.Toolbar;
import com.kaluwa.enterprises.babycare.MainActivity; import com.kaluwa.enterprises.babycare.MainActivity;
import com.kaluwa.enterprises.babycare.R; import com.kaluwa.enterprises.babycare.R;
import com.kaluwa.enterprises.babycare.activities.auth.LoginActivity;
public class DashboardActivity extends AppCompatActivity { public class DashboardActivity extends AppCompatActivity {
...@@ -41,7 +40,8 @@ public class DashboardActivity extends AppCompatActivity { ...@@ -41,7 +40,8 @@ public class DashboardActivity extends AppCompatActivity {
btn6 = findViewById(R.id.btn_settings); btn6 = findViewById(R.id.btn_settings);
btn1.setOnClickListener(v -> { btn1.setOnClickListener(v -> {
Intent intent = new Intent(this, LiveFeedActivity.class);
startActivity(intent);
}); });
btn2.setOnClickListener(v -> { btn2.setOnClickListener(v -> {
......
package com.kaluwa.enterprises.babycare.activities;
import static com.kaluwa.enterprises.babycare.config.TokenSaver.clearToken;
import static com.kaluwa.enterprises.babycare.constants.Configs.LIVE_FEED_URL;
import static com.kaluwa.enterprises.babycare.constants.Configs.REFRESH_INTERVAL;
import static com.kaluwa.enterprises.babycare.utils.Utils.animationChanger;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.Toolbar;
import com.kaluwa.enterprises.babycare.MainActivity;
import com.kaluwa.enterprises.babycare.R;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class LiveFeedActivity extends AppCompatActivity {
private static final String TAG = "LiveFeedActivity";
private ImageView ivLiveFeed;
private Button lfFlashBtn;
private TextView tvLlStatusValue;
private Handler handler = new Handler();
private Runnable updateRunnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_feed);
// define actionbar
defineActionbar();
ivLiveFeed = findViewById(R.id.ivLiveFeed);
lfFlashBtn = findViewById(R.id.lfFlashBtn);
tvLlStatusValue = findViewById(R.id.tvLlStatusValue);
updateRunnable = new Runnable() {
@Override
public void run() {
fetchAndDisplayImage();
handler.postDelayed(this, REFRESH_INTERVAL);
}
};
// Start updating
handler.post(updateRunnable);
// Toggle flashlight
lfFlashBtn.setOnClickListener(v -> toggleFlashlight());
}
private void toggleFlashlight() {
String currentText = lfFlashBtn.getText().toString();
String url = currentText.equals("FLASH ON") ? LIVE_FEED_URL+"/flashlight/on" : LIVE_FEED_URL+"/flashlight/off";
String newText = currentText.equals("FLASH ON") ? "FLASH OFF" : "FLASH ON";
// Call the API in a background thread
new Thread(() -> {
try {
URL apiUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
// Update the button text on the main thread
runOnUiThread(() -> lfFlashBtn.setText(newText));
} else {
Log.e(TAG, "Failed to call API: " + responseCode);
}
connection.disconnect();
} catch (IOException e) {
Log.e(TAG, "Error calling flashlight API", e);
}
}).start();
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(updateRunnable); // Stop updating when the activity is destroyed
}
private void fetchAndDisplayImage() {
new Thread(() -> {
try {
URL url = new URL(LIVE_FEED_URL+"/cam-hi.jpg");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
runOnUiThread(() -> ivLiveFeed.setImageBitmap(bitmap));
} catch (Exception e) {
Log.e(TAG, "Error fetching image", e);
// If there's an error, set the default image
runOnUiThread(() -> ivLiveFeed.setImageResource(R.drawable.live_offline));
}
}).start();
}
private void defineActionbar() {
Toolbar toolbar = findViewById(R.id.b_care_action_bar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
toolbar.setNavigationIcon(R.drawable.ico_menu_32);
toolbar.setNavigationOnClickListener(v -> {
// Initializing the popup menu and giving the reference as current context
PopupMenu popupMenu = new PopupMenu(this, toolbar);
popupMenu.setGravity(Gravity.BOTTOM);
popupMenu.getMenuInflater().inflate(R.menu.menu_main, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(item -> {
int id = item.getItemId();
if (id == R.id.mm_device_setting) {
Toast.makeText(this, "You Clicked " + item.getTitle(), Toast.LENGTH_SHORT).show();
} else if (id == R.id.mm_app_setting) {
Toast.makeText(this, "You Clicked " + item.getTitle(), Toast.LENGTH_SHORT).show();
} else if (id == R.id.mm_logout) {
clearToken(getApplicationContext());
Toast.makeText(this, "Logout successful.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
animationChanger(this);
}
return true;
});
popupMenu.show();
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.user_action_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.user) {
Intent intent = new Intent(this, UserProfileActivity.class);
startActivity(intent);
animationChanger(this);
} else {
Toast.makeText(this, "No item.", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
animationChanger(this);
}
}
\ No newline at end of file
package com.kaluwa.enterprises.babycare.config; package com.kaluwa.enterprises.babycare.config;
import static com.kaluwa.enterprises.babycare.config.TokenSaver.getToken; import static com.kaluwa.enterprises.babycare.config.TokenSaver.getToken;
import static com.kaluwa.enterprises.babycare.constants.Configs.BASE_URL;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
...@@ -17,7 +18,6 @@ import retrofit2.converter.gson.GsonConverterFactory; ...@@ -17,7 +18,6 @@ import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory; import retrofit2.converter.jackson.JacksonConverterFactory;
public class ApiConfig { public class ApiConfig {
private static final String BASE_URL = "http://192.168.1.2:8080/api/v1/baby-care/";
private static ApiConfig instance; private static ApiConfig instance;
private static Retrofit retrofitAuth = null; private static Retrofit retrofitAuth = null;
private static Retrofit retrofitOther = null; private static Retrofit retrofitOther = null;
......
package com.kaluwa.enterprises.babycare.constants;
public class Configs {
public static final String BASE_URL = "http://192.168.1.2:8080/api/v1/baby-care/";
public static final String LIVE_FEED_URL = "http://192.168.1.7";
public static final int REFRESH_INTERVAL = 100; // Refresh every 100 ms
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.LiveFeedActivity">
<include
layout="@layout/appbar"/>
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/b_care_action_bar"
app:layout_constraintBottom_toBottomOf="parent"
android:src="@drawable/background"
android:contentDescription="background-image"
android:scaleType="centerCrop"
android:alpha="0.4"/>
<LinearLayout
android:id="@+id/llLiveFeedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@+id/background"
app:layout_constraintStart_toStartOf="@id/background"
app:layout_constraintEnd_toEndOf="@id/background"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<ImageView
android:id="@+id/ivLiveFeed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="450dp"
android:scaleType="centerInside"
android:src="@drawable/live_offline"
android:background="@drawable/borders"/>
<Button
android:id="@+id/lfFlashBtn"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="28dp"
android:layout_marginRight="40dp"
android:background="@drawable/positive_btn_background"
android:fontFamily="@font/abril_fatface_regular"
android:padding="6dp"
android:text="FLASH ON"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>
<!-- New LinearLayout for displaying the status -->
<LinearLayout
android:id="@+id/llStatusBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/llLiveFeedBox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:orientation="horizontal"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/tvLlStatusLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Baby's Status:"
android:textColor="@color/black"
android:textSize="18sp"
android:layout_marginEnd="8dp"/>
<TextView
android:id="@+id/tvLlStatusValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Detected!"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
...@@ -152,15 +152,15 @@ ...@@ -152,15 +152,15 @@
android:id="@+id/l_btn_login" android:id="@+id/l_btn_login"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="LOGIN"
android:textSize="20sp"
android:fontFamily="@font/abril_fatface_regular"
android:layout_marginLeft="40dp" android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="28dp" android:layout_marginTop="28dp"
android:layout_marginRight="40dp"
android:background="@drawable/positive_btn_background" android:background="@drawable/positive_btn_background"
android:fontFamily="@font/abril_fatface_regular"
android:padding="6dp" android:padding="6dp"
android:textColor="@color/white"/> android:text="LOGIN"
android:textColor="@color/white"
android:textSize="20sp" />
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
......
...@@ -4,5 +4,7 @@ ...@@ -4,5 +4,7 @@
<domain includeSubdomains="true">192.168.1.2</domain> <domain includeSubdomains="true">192.168.1.2</domain>
<domain includeSubdomains="true">192.168.1.6</domain> <domain includeSubdomains="true">192.168.1.6</domain>
<domain includeSubdomains="true">192.168.63.103</domain> <domain includeSubdomains="true">192.168.63.103</domain>
<domain includeSubdomains="true">192.168.1.3</domain>
<domain includeSubdomains="true">192.168.1.7</domain>
</domain-config> </domain-config>
</network-security-config> </network-security-config>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment