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

1.Check for NFD app at the startup 2. RecyclerView for ViewFriendsActivity and SelectRecipientsActivity 3. Positive & Negative Buttons for alertDialog in requestCode = FILE_SELECT_REQUEST_CODE. #109

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ android {
exclude 'META-INF/NOTICE.txt'
}
}

}

dependencies {
Expand All @@ -49,6 +48,8 @@ dependencies {
implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
implementation 'net.named-data:jndn-android:0.18'
implementation 'commons-io:commons-io:2.6'
implementation 'com.android.support:recyclerview-v7:27.1.0'
implementation 'com.android.support:cardview-v7:27.1.0'
implementation('net.named-data.jndn-extra:jndn-management:1.2.1') {
exclude group: 'net.named-data', module: 'jndn'
}
Expand Down
28 changes: 25 additions & 3 deletions app/src/main/java/memphis/myapplication/FilesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -97,9 +98,12 @@ public FileManager getFileManager() {
public void select_files(View view) {
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
// browser.
Toast.makeText(getApplicationContext(),"OKO",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// To search for all documents available via installed storage providers,
// it would be "*/*".
// For images
// intent.setType("image/*");
intent.setType("*/*");
startActivityForResult(intent, FILE_SELECT_REQUEST_CODE);
}
Expand All @@ -114,11 +118,29 @@ public void onActivityResult(int requestCode, int resultCode,
if (requestCode == FILE_SELECT_REQUEST_CODE) {

uri = resultData.getData();
String path = getFilePath(uri);
final String path = getFilePath(uri);

if (path != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
builder.setTitle("You selected a file").setMessage(path).show();
final AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
builder.setTitle("You selected a file").setMessage(path);
builder.setPositiveButton(R.string.share_files, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
FileManager manager = new FileManager(getApplicationContext());
ArrayList<String> friendsList = manager.getFriendsList();
Intent intent = new Intent(getApplicationContext(),SelectRecipientsActivity.class);
intent.putStringArrayListExtra("friendsList", friendsList);
intent.putExtra("photo", path);
startActivity(intent);
}
}).create();
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
builder.show();
byte[] bytes;
try {
InputStream is = this.getContentResolver().openInputStream(uri);
Expand Down
57 changes: 47 additions & 10 deletions app/src/main/java/memphis/myapplication/IntroActivity.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,62 @@
package memphis.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class IntroActivity extends AppCompatActivity {

final String nfdAppPackageName = "net.named_data.nfd";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Session session = new Session(getApplicationContext());
// session checks sharedPreferences where we store our login boolean variable
// if we're not logged in, start LoginActivity
if (!session.getLoginStatus()) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
Context context = getApplicationContext();
Session session = new Session(context);

// Check if NDN Forwarding Daemon is installed.
// If installed -> Continue Regular Onboarding
// If not installed -> Show a Message and request User to install NDN Forwarding Daemon.
PackageManager pm = context.getPackageManager();
if(isPackageInstalled(nfdAppPackageName,pm))
{
// session checks sharedPreferences where we store our login boolean variable
// if we're not logged in, start LoginActivity
if (!session.getLoginStatus()) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
// we are logged in; take us to MainActivity
else {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}else{

Toast.makeText(context,"Please consider Installing NFD, this is " +
"required for npChat to work. After NFD has started, reopen the app.",
Toast.LENGTH_LONG).show();

try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + nfdAppPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + nfdAppPackageName)));
}
}
// we are logged in; take us to MainActivity
else {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

}

private boolean isPackageInstalled(String packageName, PackageManager packageManager) {
boolean found = true;
try {
packageManager.getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
found = false;
}
return found;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package memphis.myapplication;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class ListDisplayRecyclerView extends android.support.v7.widget.RecyclerView.Adapter<ListDisplayRecyclerView.ViewHolder> {

// This ListDisplayRecyclerView can be used to display a text in a cardView.
// One Example where it is used in the Project is to display FriendsList.

private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;

ListDisplayRecyclerView(Context context, List<String> data){
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}

@NonNull
@Override
public ListDisplayRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ListDisplayRecyclerView.ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ListDisplayRecyclerView.ViewHolder holder, int position) {
String friendName = mData.get(position);
holder.textView.setText(friendName);
}

@Override
public int getItemCount() {
return mData.size();
}

String getItem(int id) {
return mData.get(id);
}

// stores and recycles views as they are scrolled off screen
public class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder implements View.OnClickListener {
TextView textView;
CardView cardView;
ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.recyclerTextView);
cardView = itemView.findViewById(R.id.recyclerCardView);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}

// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
115 changes: 46 additions & 69 deletions app/src/main/java/memphis/myapplication/SelectRecipientsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class SelectRecipientsActivity extends AppCompatActivity {
public class SelectRecipientsActivity extends AppCompatActivity implements ListDisplayRecyclerView.ItemClickListener {

private ArrayList<String> m_selectedFriends;
private Button m_sendButton;
private ListDisplayRecyclerView adapter;

@Override
public void onCreate (Bundle savedInstanceState) {
Expand All @@ -32,82 +35,23 @@ public void onCreate (Bundle savedInstanceState) {

private void showFriends() {
Intent intent = getIntent();
LinearLayout linearLayout = findViewById(R.id.listLinearLayout);
String path = getIntent().getStringExtra("photo");
Toast.makeText(getApplicationContext(),"Path of Chosen File is "+path,Toast.LENGTH_LONG).show();
ArrayList<String> friendsList = intent.getStringArrayListExtra("friendsList");
m_sendButton = findViewById(R.id.send_button);

final int primary = ContextCompat.getColor(this, R.color.colorPrimary);
final int black = ContextCompat.getColor(this, R.color.jetBlack);
final int white = ContextCompat.getColor(this, R.color.white);
// if we don't have any saved friends, we have nothing to display; tell user
if(friendsList.isEmpty()) {
m_sendButton = findViewById(R.id.send_button);
m_sendButton.setVisibility(View.GONE);
TextView message = new TextView(this);
String s = "You currently haven't added any friends.";
message.setText(s);
message.setTextColor(white);
message.setTextSize(24);
message.setGravity(Gravity.LEFT);
linearLayout.addView(message);
Toast.makeText(getApplicationContext(),R.string.no_friends,Toast.LENGTH_LONG).show();
}
else {
// programmatically create TextViews to place in the LinearLayout since we don't know how
// many friends a person will have
for (String friend : friendsList) {
final TextView friendName = new TextView(this);
friendName.setText(friend);
friendName.setTextColor(white);
friendName.setTextSize(34);
// create a border for each TextView (friend slot)
final GradientDrawable drawable1 = new GradientDrawable();
drawable1.setColor(primary);
drawable1.setStroke(2, black);

final GradientDrawable drawable2 = new GradientDrawable();
drawable2.setColor(Color.parseColor("#333377"));
drawable2.setStroke(2, black);

// place border around TextView and set background
friendName.setBackground(drawable1);

friendName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("SelectRecipients", "We selected " + friendName.getText());
if (friendName.isSelected()) {
friendName.setBackground(drawable1);
friendName.setSelected(false);
m_selectedFriends.remove(friendName.getText().toString());
if(m_selectedFriends.size() < 1) {
// remove button since we have selected 0 friends now
m_sendButton.setVisibility(View.GONE);
}
Log.d("showFriends", "After removed: " + m_selectedFriends.size());
} else {
friendName.setBackground(drawable2);
friendName.setSelected(true);
m_selectedFriends.add(friendName.getText().toString());
if(m_selectedFriends.size() == 1) {
// only need to set visibility for button when we add the first friend
m_sendButton.setVisibility(View.VISIBLE);
}
Log.d("showFriends", "After add: " + m_selectedFriends.size());
}
}
});
// Add TextView to LinearLayout
linearLayout.addView(friendName);
}

m_sendButton = findViewById(R.id.send_button);
// make send button invisible and unclickable until we actually select a friend
android.support.v7.widget.RecyclerView recyclerView = findViewById(R.id.friendList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new ListDisplayRecyclerView(this, friendsList);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
m_sendButton.setVisibility(View.GONE);
m_sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
returnList();
}
});
}
}

Expand Down Expand Up @@ -156,4 +100,37 @@ public void onClick(DialogInterface dialogInterface, int i) {

question.show();
}

@Override
public void onItemClick(View view, int position) {
// Toast.makeText(this, "Selected Friend Name: " + adapter.getItem(position),Toast.LENGTH_SHORT).show();
// Assumes Each Friend Name will be unique.
// This condition fails if duplicates exist in friendList
if(!m_selectedFriends.contains(adapter.getItem(position))){
m_selectedFriends.add(adapter.getItem(position));
view.setBackgroundColor(Color.CYAN);
Log.d("SelectRecipients", "We selected " + adapter.getItem(position));
Log.d("showFriends", "After add: " + m_selectedFriends.size());
}else{
m_selectedFriends.remove(adapter.getItem(position));
view.setBackgroundColor(Color.TRANSPARENT);
Log.d("SelectRecipients", "We deselected " + adapter.getItem(position));
Log.d("showFriends", "After removed: " + m_selectedFriends.size());
}

if(m_selectedFriends.size() == 0) {
// remove button since we have selected 0 friends now
m_sendButton.setVisibility(View.GONE);
}else{
m_sendButton.setVisibility(View.VISIBLE);
}

m_sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
returnList();
}
});
}
}

Loading