diff --git a/app/build.gradle b/app/build.gradle index e008c28..aadb45f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,7 +34,6 @@ android { exclude 'META-INF/NOTICE.txt' } } - } dependencies { @@ -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' } diff --git a/app/src/main/java/memphis/myapplication/FilesActivity.java b/app/src/main/java/memphis/myapplication/FilesActivity.java index 22a9809..48f38d6 100644 --- a/app/src/main/java/memphis/myapplication/FilesActivity.java +++ b/app/src/main/java/memphis/myapplication/FilesActivity.java @@ -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; @@ -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); } @@ -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 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); diff --git a/app/src/main/java/memphis/myapplication/IntroActivity.java b/app/src/main/java/memphis/myapplication/IntroActivity.java index 07092cc..31bc65e 100644 --- a/app/src/main/java/memphis/myapplication/IntroActivity.java +++ b/app/src/main/java/memphis/myapplication/IntroActivity.java @@ -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; } } + diff --git a/app/src/main/java/memphis/myapplication/ListDisplayRecyclerView.java b/app/src/main/java/memphis/myapplication/ListDisplayRecyclerView.java new file mode 100644 index 0000000..4972760 --- /dev/null +++ b/app/src/main/java/memphis/myapplication/ListDisplayRecyclerView.java @@ -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 { + + // 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 mData; + private LayoutInflater mInflater; + private ItemClickListener mClickListener; + + ListDisplayRecyclerView(Context context, List 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); + } +} diff --git a/app/src/main/java/memphis/myapplication/SelectRecipientsActivity.java b/app/src/main/java/memphis/myapplication/SelectRecipientsActivity.java index 6a1120f..d5cf0e0 100644 --- a/app/src/main/java/memphis/myapplication/SelectRecipientsActivity.java +++ b/app/src/main/java/memphis/myapplication/SelectRecipientsActivity.java @@ -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 m_selectedFriends; private Button m_sendButton; + private ListDisplayRecyclerView adapter; @Override public void onCreate (Bundle savedInstanceState) { @@ -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 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(); - } - }); } } @@ -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(); + } + }); + } } + diff --git a/app/src/main/java/memphis/myapplication/ViewFriendsActivity.java b/app/src/main/java/memphis/myapplication/ViewFriendsActivity.java index 2bcda36..a83d6ea 100644 --- a/app/src/main/java/memphis/myapplication/ViewFriendsActivity.java +++ b/app/src/main/java/memphis/myapplication/ViewFriendsActivity.java @@ -1,62 +1,39 @@ package memphis.myapplication; import android.content.Intent; -import android.graphics.drawable.GradientDrawable; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; -import android.util.Log; +import android.support.v7.widget.LinearLayoutManager; import android.view.Gravity; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import java.util.ArrayList; -public class ViewFriendsActivity extends AppCompatActivity { +public class ViewFriendsActivity extends AppCompatActivity implements ListDisplayRecyclerView.ItemClickListener { - // add border around each friend; increase font size; change colors + ListDisplayRecyclerView adapter; @Override protected void onCreate(Bundle savedInstanceBundle) { super.onCreate(savedInstanceBundle); setContentView(R.layout.activity_with_list); + Intent intent = getIntent(); - int count = 0; - LinearLayout linearLayout = findViewById(R.id.listLinearLayout); ArrayList friendsList = intent.getStringArrayListExtra("friendsList"); - - int accent = ContextCompat.getColor(this, R.color.colorPrimary); - int black = ContextCompat.getColor(this, R.color.jetBlack); - 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()) { - TextView message = new TextView(this); - message.setText(R.string.no_friends); - message.setTextColor(white); - message.setTextSize(24); - message.setGravity(Gravity.CENTER); - linearLayout.setGravity(Gravity.CENTER); - 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) { - Log.d("ViewFriends", "Friend" + count + " " + friend); - TextView friendName = new TextView(this); - friendName.setText(friend); - friendName.setTextColor(white); - friendName.setTextSize(34); - // create a border for each TextView (friend slot) - GradientDrawable drawable = new GradientDrawable(); - drawable.setColor(accent); - drawable.setStroke(2, black); - // place border around TextView - friendName.setBackground(drawable); - // Add TextView to LinearLayout - linearLayout.addView(friendName); - count++; - } + // set up the ListDisplayRecyclerView, and display the list of friend in ListDisplayRecyclerView + 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); } } @@ -64,6 +41,13 @@ protected void onCreate(Bundle savedInstanceBundle) { // here or link off to a different page where you can view their profile and remove them, if you want? public void removeFriend(View view) { FileManager manager = new FileManager(getApplicationContext()); + } + @Override + public void onItemClick(View view, int position) { + // The Functionality to Link off to a different page can be handled here. + // OnClick of an Item in the recyclerView. + Toast.makeText(this, "Friend Name: " + adapter.getItem(position) + + " Count: " + position, Toast.LENGTH_SHORT).show(); } } diff --git a/app/src/main/res/layout/activity_select_recipients.xml b/app/src/main/res/layout/activity_select_recipients.xml index 7282ff3..42c8807 100644 --- a/app/src/main/res/layout/activity_select_recipients.xml +++ b/app/src/main/res/layout/activity_select_recipients.xml @@ -12,11 +12,18 @@ android:orientation="vertical" android:background="@color/colorPrimary" android:clickable="true" + android:focusable="true" android:elevation="@dimen/margin_0dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" > + +