Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Feature/pictures import #166

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ch.epfl.sdp.appart">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
Expand All @@ -17,11 +17,12 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".databus.Activity2"/>
<activity android:name=".databus.Activity1"/>
<activity android:name=".PicturesImportActivity" />
<activity android:name=".MapActivity"
android:exported="true">
</activity>
<activity
android:name=".MapActivity"
android:exported="true" />
<activity android:name=".LocationActivity" />
<activity android:name=".SimpleUserProfileActivity" />
<activity android:name=".CameraActivity" />
Expand All @@ -44,10 +45,9 @@
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${mapsApiKey}" />

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

</application>

</manifest>
5 changes: 3 additions & 2 deletions app/src/main/java/ch/epfl/sdp/appart/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;

import ch.epfl.sdp.appart.databus.Activity1;

/**
* The main UI class.
*/
Expand All @@ -17,7 +18,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = this.getIntent().getExtras();
Intent intent = new Intent(this, LoginActivity.class);
Intent intent = new Intent(this, Activity1.class);
if(extras != null && extras.containsKey("email") && extras.containsKey("password")){
intent.putExtra("email", extras.getString("email"));
intent.putExtra("password", extras.getString("password"));
Expand Down
73 changes: 73 additions & 0 deletions app/src/main/java/ch/epfl/sdp/appart/databus/Activity1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ch.epfl.sdp.appart.databus;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;

import ch.epfl.sdp.appart.R;
import ch.epfl.sdp.appart.hilt.databus.annotations.IntegerDataBus;
import ch.epfl.sdp.appart.hilt.databus.annotations.StringDataBus;
import ch.epfl.sdp.appart.hilt.databus.annotations.UriListDataBus;
import dagger.hilt.android.AndroidEntryPoint;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

@AndroidEntryPoint
public class Activity1 extends AppCompatActivity {

@IntegerDataBus
@Inject
DataBus<Integer> integerBus;

@StringDataBus
@Inject
DataBus<String> stringBus;

@UriListDataBus
@Inject
DataBus<List<Uri>> uriListBus;

@UriListDataBus
@Inject
PrivateDataBus<List<Uri>> uriPrivateDataBus;

private PrivateDataBusToken token =
PrivateDataBusTokenFactory.makeToken(Activity1.class, Activity2.class);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Button gotoActivity2Button = findViewById(R.id.goto_activity_2_button);

/**
* Solution 1 : basic bus
*/
integerBus.setData(1234);
stringBus.setData("coucou !");
uriListBus.setData(Arrays.asList(Uri.parse("abc"), Uri.parse("1234"), Uri.parse("cool")));

/**
* Solution 2 : private bus
*/
uriPrivateDataBus.setData(token, Arrays.asList(Uri.parse("so"), Uri.parse("private")));

gotoActivity2Button.setOnClickListener(v -> {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
});

}

@Override
protected void onResume() {
super.onResume();
}
}
66 changes: 66 additions & 0 deletions app/src/main/java/ch/epfl/sdp/appart/databus/Activity2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ch.epfl.sdp.appart.databus;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;

import java.util.List;

import javax.inject.Inject;

import ch.epfl.sdp.appart.R;
import ch.epfl.sdp.appart.hilt.databus.annotations.IntegerDataBus;
import ch.epfl.sdp.appart.hilt.databus.annotations.StringDataBus;
import ch.epfl.sdp.appart.hilt.databus.annotations.UriListDataBus;
import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class Activity2 extends AppCompatActivity {

/**
* Bellow we declare the bus we want to use
*/
@IntegerDataBus
@Inject
DataBus<Integer> integerBus;

@StringDataBus
@Inject
DataBus<String> stringBus;

@UriListDataBus
@Inject
DataBus<List<Uri>> uriListBus;

/**
* Solution 2 : private databus
*/

@UriListDataBus
@Inject
PrivateDataBus<List<Uri>> uriPrivateDataBus;

private PrivateDataBusToken token =
PrivateDataBusTokenFactory.makeToken(Activity1.class, Activity2.class);

private Integer a;
private String b;
private List<Uri> c;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);

/**
* Solution 1 and 2 get data
*/
a = integerBus.getData();
b = stringBus.getData();
c = uriListBus.getData();

c = uriPrivateDataBus.getData(token);

}
}
37 changes: 37 additions & 0 deletions app/src/main/java/ch/epfl/sdp/appart/databus/DataBus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ch.epfl.sdp.appart.databus;

import javax.inject.Inject;
import javax.inject.Singleton;

/**
* Solution 1
* Class that allows activities to talk to each other with a bus. All classes that
* utilise this class defined for some type T will utilize the same class.
* @param <T> The type of the data that will be exchangeable over the bus.
* This class is only meant to be used via hilt injection and should never be instantiated.
*/
@Singleton
public class DataBus<T> {

T data;

@Inject
public DataBus() {}

/**
* Set the data contained in the bus.
* @param data data T to be set.
*/
public void setData(T data) {
this.data = data;
}

/**
* Get the data contained in the bus.
* @return T data
*/
public T getData() {
return data;
}

}
80 changes: 80 additions & 0 deletions app/src/main/java/ch/epfl/sdp/appart/databus/ExclusiveDataBus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ch.epfl.sdp.appart.databus;

/**
* Solution 2
* This class allows for two classes to exchange data by allowing exactly two classes to
* talk with each other.
* @param <T> the type of parameters
*/
public class ExclusiveDataBus<T> {

private T data;
private Class<?> firstTalker;
private Class<?> secondTalker;

/**
* Set the owner ship of the bus for one of the talker
* @param newTalker
*/
public void bind(Class<?> newTalker) {
if (firstTalker == null) {
firstTalker = newTalker;
return;
}

if (secondTalker == null) {
secondTalker = newTalker;
return;
}

throw new IllegalStateException("bus already owned by " + firstTalker.getCanonicalName()
+ " and " + secondTalker.getCanonicalName() + " but requested by " + newTalker.getCanonicalName());
}

public void release(Class<?> talker) {

if (firstTalker.getCanonicalName().equals(talker.getCanonicalName())) {
firstTalker = null;
return;
}

if (secondTalker.getCanonicalName().equals(talker.getCanonicalName())) {
secondTalker = null;
return;
}

throw new IllegalStateException("bus owned by " + firstTalker.getCanonicalName()
+ " and " + secondTalker.getCanonicalName() + " but trying to be released by "
+ talker.getCanonicalName());

}

public void setData(Class<?> talker, T data) {
if (!firstTalker.getCanonicalName().equals(talker.getCanonicalName()) &&
!secondTalker.getCanonicalName().equals(talker.getCanonicalName())) {
throw new IllegalStateException("bus already owned by " + firstTalker.getCanonicalName()
+ " and " + secondTalker.getCanonicalName() + " but accessed by " + talker.getCanonicalName());
}

if (firstTalker == null || secondTalker == null) {
throw new IllegalStateException("Two talkers must be registered");
}

this.data = data;
}

public T getData(Class<?> talker) {
if (!firstTalker.getCanonicalName().equals(talker.getCanonicalName()) &&
!secondTalker.getCanonicalName().equals(talker.getCanonicalName())) {
throw new IllegalStateException("bus already owned by " + firstTalker.getCanonicalName()
+ " and " + secondTalker.getCanonicalName() + " but read by " + talker.getCanonicalName());
}

if (firstTalker == null || secondTalker == null) {
throw new IllegalStateException("Two talkers must be registered");
}

return data;
}

}
44 changes: 44 additions & 0 deletions app/src/main/java/ch/epfl/sdp/appart/databus/PrivateDataBus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ch.epfl.sdp.appart.databus;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;

import javax.inject.Inject;

/**
* This class allows activities to talk with the use of tokens.
* A token equals a channel and enforce that to see a value you need to have the right token
* See the token factory to see how it is synthesized.
* @param <T>
*/
public class PrivateDataBus<T> {

private HashMap<PrivateDataBusToken, T> entries = new HashMap<>();

@Inject
public PrivateDataBus() {}

/**
* Set the data contained in the bus.
* @param data data T to be set.
*/
public <W, X> void setData(@NonNull PrivateDataBusToken token, @Nullable T data) {
if (token == null) {
throw new IllegalArgumentException("token cannot be null");
}
entries.put(token, data);
}

/**
* Get the data contained in the bus.
* @return T data
*/
public <W, X> T getData(@NonNull PrivateDataBusToken token) {
if (token == null) {
throw new IllegalArgumentException("token cannot be null");
}
return entries.getOrDefault(token, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ch.epfl.sdp.appart.databus;

public class PrivateDataBusToken {
private final int token;
public PrivateDataBusToken(int token) { this.token = token; }
protected int getToken() { return token; }

@Override
public int hashCode() {
return token;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other == null) {
return false;
} else if (getClass() != other.getClass()) {
return false;
} else {
PrivateDataBusToken t = (PrivateDataBusToken) other;
return t.getToken() == getToken();
}
}
}
Loading