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

menu: add upgrade notification (fixes #536)(fixes #132) #538

Merged
merged 11 commits into from
Jan 2, 2020
108 changes: 52 additions & 56 deletions app/src/main/java/io/treehouses/remote/Fragments/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.drawable.AnimationDrawable;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -25,7 +22,6 @@
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.location.LocationManagerCompat;

import java.util.Arrays;
import java.util.Calendar;
Expand All @@ -39,30 +35,28 @@
import io.treehouses.remote.Network.ParseDbService;
import io.treehouses.remote.R;
import io.treehouses.remote.bases.BaseFragment;
import io.treehouses.remote.callback.NotificationCallback;
import io.treehouses.remote.callback.SetDisconnect;
import io.treehouses.remote.utils.LogUtils;
import io.treehouses.remote.utils.Utils;
import io.treehouses.remote.utils.VersionUtils;

import com.parse.ParseObject;

import static io.treehouses.remote.Constants.REQUEST_ENABLE_BT;

public class HomeFragment extends BaseFragment implements SetDisconnect {
private static final String TAG = "HOME_FRAGMENT";

private NotificationCallback notificationListener;

private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private BluetoothChatService mChatService = null;
private Button connectRpi, getStarted, testConnection;
private Boolean connectionState = false;
private Boolean result = false;
private AlertDialog testConnectionDialog;
private int selected_LED;
View view;
SharedPreferences preferences;
private SharedPreferences preferences;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_home_fragment, container, false);
View view = inflater.inflate(R.layout.activity_home_fragment, container, false);
mChatService = listener.getChatService();
connectRpi = view.findViewById(R.id.btn_connect);
getStarted = view.findViewById(R.id.btn_getStarted);
Expand All @@ -79,29 +73,23 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (MainApplication.showLogDialog) {
showLogDialog();
}
if (MainApplication.showLogDialog) { showLogDialog(); }
}

private void showLogDialog() {
int connectionCount = preferences.getInt("connection_count", 0);
boolean showDialog = preferences.getBoolean("show_log_dialog", true);
LogUtils.log(connectionCount + " " + showDialog);
long lastDialogShown = preferences.getLong("last_dialog_shown", 0);
Calendar date = Calendar.getInstance();
date.add(Calendar.DAY_OF_YEAR, -7);
if (lastDialogShown < date.getTimeInMillis()) {
if (connectionCount >= 3 && showDialog) {
preferences.edit().putLong("last_dialog_shown", Calendar.getInstance().getTimeInMillis()).commit();
new AlertDialog.Builder(getActivity()).setTitle("Alert !!!!").setCancelable(false).setMessage("Treehouses wants to collect your activities. " +
"Do you like to share it? It will help us to improve.")
.setPositiveButton("Yes", (dialogInterface, i) -> {
preferences.edit().putBoolean("send_log", true).commit();
preferences.edit().putBoolean("show_log_dialog", false).commit();
})
.setNegativeButton("No", (dialogInterface, i) -> MainApplication.showLogDialog = false).show();
}
if (lastDialogShown < date.getTimeInMillis() && connectionCount >= 3 && showDialog) {
preferences.edit().putLong("last_dialog_shown", Calendar.getInstance().getTimeInMillis()).commit();
new AlertDialog.Builder(getActivity()).setTitle("Alert !!!!").setCancelable(false).setMessage("Treehouses wants to collect your activities. " +
"Do you like to share it? It will help us to improve.")
.setPositiveButton("Yes", (dialogInterface, i) -> {
preferences.edit().putBoolean("send_log", true).commit();
preferences.edit().putBoolean("show_log_dialog", false).commit();
}).setNegativeButton("No", (dialogInterface, i) -> MainApplication.showLogDialog = false).show();
}
}

Expand All @@ -121,22 +109,22 @@ private void getStartedListener() {
}

public void connectRpiListener() {
connectRpi.setOnClickListener(v -> {
if (connectionState) {
RPIDialogFragment.getInstance().bluetoothCheck("unregister");
mChatService.stop();
connectionState = false;
checkConnectionState();
return;
}
connectRpi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (connectionState) {
RPIDialogFragment.getInstance().bluetoothCheck("unregister");
mChatService.stop();
connectionState = false;
checkConnectionState();
return;
}

if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Toast.makeText(getContext(), "Bluetooth is disabled", Toast.LENGTH_LONG).show();
return;
} else if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
showRPIDialog();
if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Toast.makeText(getContext(), "Bluetooth is disabled", Toast.LENGTH_LONG).show();
} else if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) { showRPIDialog(); }
}
});
}
Expand All @@ -162,6 +150,8 @@ public void checkConnectionState() {
connectRpi.setText("Disconnect");
connectionState = true;
testConnection.setVisibility(View.VISIBLE);
writeToRPI("treehouses upgrade --check"); //Check upgrade status

} else {
connectRpi.setText("Connect to RPI");
connectionState = false;
Expand All @@ -183,11 +173,8 @@ private AlertDialog showWelcomeDialog() {
final SpannableString s = new SpannableString("Treehouses Remote only works with our treehouses images, or a raspbian image enhanced by \"control\" and \"cli\". There is more information under \"Get Started\"" +
"\n\nhttp://download.treehouses.io\nhttps://github.com/treehouses/control\nhttps://github.com/treehouses/cli");
Linkify.addLinks(s, Linkify.ALL);
final AlertDialog d = new AlertDialog.Builder(getContext())
.setTitle("Friendly Reminder")
.setIcon(R.drawable.dialog_icon)
.setNegativeButton("OK", (dialog, which) -> dialog.cancel())
.setMessage(s)
final AlertDialog d = new AlertDialog.Builder(getContext()).setTitle("Friendly Reminder").setIcon(R.drawable.dialog_icon)
.setNegativeButton("OK", (dialog, which) -> dialog.cancel()).setMessage(s)
.create();
d.show();
((TextView) d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
Expand Down Expand Up @@ -248,9 +235,21 @@ private void dismissTestConnection() {
}
}

private void writeToRPI(String ping) {
byte[] pSend = ping.getBytes();
mChatService.write(pSend);
private boolean checkUpgrade(String s) {
if (!s.isEmpty() && s.contains("true") || s.contains("false")) {
notificationListener.setNotification(s.contains("true"));
return true;
}
return false;
}

private void writeToRPI(String ping) { mChatService.write(ping.getBytes()); }

@Override
public void onAttach(Context context) {
super.onAttach(context);
try { notificationListener = (NotificationCallback) getContext();
} catch (ClassCastException e) { throw new ClassCastException("Activity must implement NotificationListener"); }
}

/**
Expand All @@ -260,13 +259,10 @@ private void writeToRPI(String ping) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Constants.MESSAGE_WRITE:
String writeMessage = new String((byte[]) msg.obj);
Log.d(TAG, "WRITTEN: " + writeMessage);
break;
case Constants.MESSAGE_READ:
String readMessage = (String) msg.obj;
if (!readMessage.isEmpty() && !result) {

if (!readMessage.isEmpty() && !checkUpgrade(readMessage) && !result) {
result = true;
dismissTestConnection();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.treehouses.remote.Fragments;

import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentActivity;
import io.treehouses.remote.Constants;
import io.treehouses.remote.Network.BluetoothChatService;
import io.treehouses.remote.R;
import io.treehouses.remote.bases.BaseFragment;

import io.treehouses.remote.callback.NotificationCallback;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -27,8 +29,6 @@
import java.util.List;

public class StatusFragment extends BaseFragment {
public StatusFragment() {}

View view;

private static final String TAG = "StatusFragment";
Expand All @@ -43,6 +43,7 @@ public StatusFragment() {}
private BluetoothChatService mChatService = null;
private CardView cardRPIName;

private NotificationCallback notificationListener;
/**
* Name of the connected device
*/
Expand Down Expand Up @@ -197,6 +198,7 @@ private void checkUpgradeStatus() {
updateRightNow = false;
pd.dismiss();
Toast.makeText(getContext(), "Treehouses Cli has been updated!!!", Toast.LENGTH_LONG).show();
notificationListener.setNotification(false);
}
if (outs.get(6).equals("false ")) {
ivUpgrade.setImageDrawable(getResources().getDrawable(R.drawable.tick));
Expand Down Expand Up @@ -242,15 +244,22 @@ public void onClick(DialogInterface dialog, int which) {
})
.create();
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
notificationListener = (NotificationCallback) getContext();
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement NotificationListener");
}
}

/**
* The Handler that gets information back from the BluetoothChatService
*/
public final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
FragmentActivity activity = getActivity();
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
checkStatusNow();
Expand Down
24 changes: 15 additions & 9 deletions app/src/main/java/io/treehouses/remote/InitialActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
Expand All @@ -36,7 +35,11 @@
import io.treehouses.remote.Network.BluetoothChatService;
import io.treehouses.remote.bases.PermissionActivity;
import io.treehouses.remote.callback.HomeInteractListener;

import io.treehouses.remote.callback.NotificationCallback;

import io.treehouses.remote.utils.GPSService;

import io.treehouses.remote.utils.LogUtils;

import android.view.Menu;
Expand All @@ -45,13 +48,14 @@


public class InitialActivity extends PermissionActivity
implements NavigationView.OnNavigationItemSelectedListener, HomeInteractListener {
implements NavigationView.OnNavigationItemSelectedListener, HomeInteractListener, NotificationCallback {

private static InitialActivity instance = null;
private Boolean validBluetoothConnection = false;
int REQUEST_COARSE_LOCATION = 99;
private static BluetoothChatService mChatService = null;
private String mConnectedDeviceName = null;
private NavigationView navigationView;
DrawerLayout drawer;
private String TAG = "InitialActivity";

Expand All @@ -62,7 +66,6 @@ protected void onCreate(Bundle savedInstanceState) {
instance = this;
setContentView(R.layout.activity_initial2);
requestPermission();
Log.e(TAG, "onCreate(Bundle) called");
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Expand All @@ -82,13 +85,13 @@ protected void onCreate(Bundle savedInstanceState) {

openCallFragment(new HomeFragment());

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = findViewById(R.id.nav_view);
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
// navigationView.addHeaderView(getResources().getLayout(R.layout.navigation_view_header));
new GPSService(this);
}
Expand All @@ -103,9 +106,7 @@ public void onBackPressed() {
drawer.closeDrawer(GravityCompat.START);
} else {
Fragment f = (getSupportFragmentManager()).findFragmentById(R.id.fragment_container);
if(f instanceof HomeFragment){
finish();
}
if(f instanceof HomeFragment) finish();
super.onBackPressed();
}
}
Expand Down Expand Up @@ -177,6 +178,11 @@ public void openCallFragment(androidx.fragment.app.Fragment newfragment) {

}
//
@Override
public void setNotification(Boolean b) {
if (b) navigationView.getMenu().getItem(7).setIcon(R.drawable.status_notification);
else navigationView.getMenu().getItem(7).setIcon(R.drawable.status);
}

protected void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.treehouses.remote.callback;

public interface NotificationCallback {
void setNotification(Boolean notificationStatus);
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_apps_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M4,8h4L8,4L4,4v4zM10,20h4v-4h-4v4zM4,20h4v-4L4,16v4zM4,14h4v-4L4,10v4zM10,14h4v-4h-4v4zM16,4v4h4L20,4h-4zM10,8h4L14,4h-4v4zM16,14h4v-4h-4v4zM16,20h4v-4h-4v4z"/>
</vector>
Binary file added app/src/main/res/drawable/status.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/status_notification.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_initial2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
app:headerLayout="@layout/navigation_view_header"
app:menu="@menu/activity_initial2_drawer"
android:background="@color/colorPrimary"
app:itemTextColor="@color/md_white_1000"
app:itemIconTint="@color/md_white_1000"/>
app:itemTextColor="@color/md_white_1000" />

</androidx.drawerlayout.widget.DrawerLayout>
4 changes: 2 additions & 2 deletions app/src/main/res/menu/activity_initial2_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
android:title="@string/menu_terminal" />
<item
android:id="@+id/menu_services"
android:icon="@drawable/circle"
android:icon="@drawable/ic_apps_black_24dp"
android:title="@string/menu_services" />
<item
android:id="@+id/menu_tunnel"
Expand All @@ -34,7 +34,7 @@
android:title="@string/menu_about" />
<item
android:id="@+id/menu_status"
android:icon="@drawable/about"
android:icon="@drawable/status"
android:title="@string/menu_status" />
</group>

Expand Down