Skip to content

Commit

Permalink
Fixed occurence of multiple MenuItems on panel sliding
Browse files Browse the repository at this point in the history
  • Loading branch information
cuttingedge03 committed Mar 25, 2014
1 parent 8b9e310 commit 0de4040
Showing 1 changed file with 59 additions and 56 deletions.
115 changes: 59 additions & 56 deletions app/src/main/java/im/tox/antox/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class MainActivity extends ActionBarActivity {
private ContactsFragment contacts;
private IntentFilter filter;
private boolean tempRightPaneActive;
MenuItem ag;

/**
* Stores all friend details and used by the contactsAdapter for displaying
Expand All @@ -108,7 +109,7 @@ public void onReceive(Context context, Intent intent) {
Log.d(TAG, "broadcast received");
String action = intent.getStringExtra("action");
if (action != null) {
Log.d(TAG, "action: " + action);
Log.d(TAG, "action: " + action);
if (action.equals(Constants.FRIEND_REQUEST)) {

} else if (action.equals(Constants.UPDATE_LEFT_PANE)) {
Expand All @@ -123,9 +124,9 @@ public void onReceive(Context context, Intent intent) {
} else if (action.equals(Constants.UPDATE_MESSAGES)) {
Log.d(TAG, "UPDATE_MESSAGES, intent key = " + intent.getStringExtra("key") + ", activeFriendKey = " + toxSingleton.activeFriendKey);
updateLeftPane();
if (intent.getStringExtra("key").equals(toxSingleton.activeFriendKey)) {
updateChat(toxSingleton.activeFriendKey);
}
if (intent.getStringExtra("key").equals(toxSingleton.activeFriendKey)) {
updateChat(toxSingleton.activeFriendKey);
}
} else if (action.equals(Constants.ACCEPT_FRIEND_REQUEST)) {
updateLeftPane();
Context ctx = getApplicationContext();
Expand All @@ -147,10 +148,10 @@ public void onReceive(Context context, Intent intent) {
};


public void updateChat(String key) {
public void updateChat(String key) {
Log.d(TAG, "updating chat");
if(toxSingleton.friendsList.getById(key)!=null
&& toxSingleton.friendsList.getById(key).getName()!=null ){
if (toxSingleton.friendsList.getById(key) != null
&& toxSingleton.friendsList.getById(key).getName() != null) {
AntoxDB db = new AntoxDB(this);
if (toxSingleton.rightPaneActive) {
db.markIncomingMessagesRead(key);
Expand Down Expand Up @@ -192,7 +193,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/* Fix for an android 4.1.x bug */
if(Build.VERSION.SDK_INT != Build.VERSION_CODES.JELLY_BEAN
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
Expand All @@ -214,29 +215,27 @@ protected void onCreate(Bundle savedInstanceState) {
}

Log.i(TAG, "onCreate");
toxSingleton.activeFriendKey=null;
toxSingleton.activeFriendRequestKey=null;
toxSingleton.activeFriendKey = null;
toxSingleton.activeFriendRequestKey = null;
setContentView(R.layout.activity_main);

toxSingleton.leftPaneActive = true;

/* Check if connected to the Internet */
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
{
if (networkInfo != null && networkInfo.isConnected()) {
// Executes in a separate thread so UI experience isn't affected
// Downloads the DHT node details
if(DhtNode.ipv4.size() == 0)
// Downloads the DHT node details
if (DhtNode.ipv4.size() == 0)
new DHTNodeDetails().execute();
}
else {
} else {
showAlertDialog(MainActivity.this, getString(R.string.main_no_internet),
getString(R.string.main_not_connected));
}

/* If the tox service isn't already running, start it */
if(!isToxServiceRunning()) {
if (!isToxServiceRunning()) {
/* If the service wasn't running then we wouldn't have gotten callbacks for a user
* going offline so default everyone to offline and just wait for callbacks.
*/
Expand Down Expand Up @@ -293,18 +292,19 @@ protected void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}

private Message mostRecentMessage(String key, ArrayList<Message> messages) {
for (int i=0; i<messages.size(); i++) {
for (int i = 0; i < messages.size(); i++) {
if (key.equals(messages.get(i).key)) {
return messages.get(i);
}
}
return new Message(-1, key, "", false, true, true, true, new Timestamp(0,0,0,0,0,0,0));
return new Message(-1, key, "", false, true, true, true, new Timestamp(0, 0, 0, 0, 0, 0, 0));
}

private int countUnreadMessages(String key, ArrayList<Message> messages) {
int counter = 0;
if(key!=null) {
if (key != null) {
Message m;
for (int i = 0; i < messages.size(); i++) {
m = messages.get(i);
Expand All @@ -319,6 +319,7 @@ private int countUnreadMessages(String key, ArrayList<Message> messages) {
}
return counter;
}

public void updateLeftPane() {

AntoxDB antoxDB = new AntoxDB(this);
Expand Down Expand Up @@ -381,6 +382,7 @@ private void openSettings() {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}

/**
* Starts a new intent to open the SettingsActivity class
*
Expand All @@ -390,6 +392,7 @@ private void openProfile() {
Intent intent = new Intent(this, ProfileActivity.class);
startActivity(intent);
}

/**
* Starts a new intent to open the AboutActivity class
*
Expand All @@ -399,6 +402,7 @@ private void openAbout() {
Intent intent = new Intent(this, AboutActivity.class);
startActivity(intent);
}

/**
* Starts a new intent to open the AddFriendActivity class
*
Expand All @@ -409,7 +413,7 @@ private void addFriend() {
startActivityForResult(intent, Constants.ADD_FRIEND_REQUEST_CODE);
}

private void clearUselessNotifications () {
private void clearUselessNotifications() {
if (toxSingleton.rightPaneActive && toxSingleton.activeFriendKey != null
&& toxSingleton.friendsList.all().size() > 0) {
AntoxFriend friend = toxSingleton.friendsList.getById(toxSingleton.activeFriendKey);
Expand Down Expand Up @@ -480,14 +484,16 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

private void addFriendToGroup() {
Log.v("Add friend to group method","To implement");
Log.v("Add friend to group method", "To implement");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
ag = menu.add(666, 100, 100, R.string.add_to_group);
ag.setIcon(R.drawable.ic_action_add_group).setVisible(false);
final MenuItem menuItem = menu.findItem(R.id.search_friend);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView
Expand Down Expand Up @@ -516,7 +522,7 @@ public void onFocusChange(View v, boolean hasFocus) {
}
});
//the class menu property is now the initialized menu
this.menu=menu;
this.menu = menu;

return true;
}
Expand All @@ -543,8 +549,7 @@ public void onClick(DialogInterface dialog, int which) {

// Downloads the the first working DHT node
private class DHTNodeDetails extends AsyncTask<Void, Void, Void> {
final String[] nodeDetails = new String[7];

final String[] nodeDetails = new String[7];


@Override
Expand All @@ -554,16 +559,14 @@ protected Void doInBackground(Void... params) {
Document document = Jsoup.connect("http://wiki.tox.im/Nodes").timeout(10000).get();
Elements nodeRows = document.getElementsByTag("tr");

for(Element nodeRow : nodeRows)
{
for (Element nodeRow : nodeRows) {
Elements nodeElements = nodeRow.getElementsByTag("td");
int c = 0;
for(Element nodeElement : nodeElements)
nodeDetails[c++]=nodeElement.text();
for (Element nodeElement : nodeElements)
nodeDetails[c++] = nodeElement.text();


if(nodeDetails[6]!=null && nodeDetails[6].equals("WORK"))
{
if (nodeDetails[6] != null && nodeDetails[6].equals("WORK")) {
DhtNode.ipv4.add(nodeDetails[0]);
DhtNode.ipv6.add(nodeDetails[1]);
DhtNode.port.add(nodeDetails[2]);
Expand All @@ -578,13 +581,13 @@ protected Void doInBackground(Void... params) {

Log.d(TAG, "About to ping servers...");
/**
* Ping servers to find quickest connection
*/
* Ping servers to find quickest connection
*/
long shortestTime = 99999;
int pos = -1;
Socket socket = null;
Log.d(TAG, "DhtNode size: " + DhtNode.ipv4.size());
for(int i = 0;i < DhtNode.ipv4.size(); i++) {
for (int i = 0; i < DhtNode.ipv4.size(); i++) {
Log.d(TAG, "i = " + i);
try {
long currentTime = System.currentTimeMillis();
Expand All @@ -603,7 +606,7 @@ protected Void doInBackground(Void... params) {
}
}

if(socket != null) {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
Expand All @@ -612,7 +615,7 @@ protected Void doInBackground(Void... params) {
}

/* Move quickest node to front of list */
if(pos != -1) {
if (pos != -1) {
DhtNode.ipv4.add(0, DhtNode.ipv4.get(pos));
DhtNode.ipv6.add(0, DhtNode.ipv6.get(pos));
DhtNode.port.add(0, DhtNode.port.get(pos));
Expand All @@ -626,8 +629,7 @@ protected Void doInBackground(Void... params) {
}

@Override
protected void onPostExecute(Void result)
{
protected void onPostExecute(Void result) {
try {
//Checking the details
System.out.println("node details:");
Expand All @@ -637,24 +639,23 @@ protected void onPostExecute(Void result)
System.out.println(DhtNode.key);
System.out.println(DhtNode.owner);
System.out.println(DhtNode.location);
}catch (NullPointerException e){
Toast.makeText(MainActivity.this,getString(R.string.main_node_list_download_error),Toast.LENGTH_SHORT).show();
} catch (NullPointerException e) {
Toast.makeText(MainActivity.this, getString(R.string.main_node_list_download_error), Toast.LENGTH_SHORT).show();
}
/**
* There is a chance that downloading finishes later than the bootstrapping call in the
* ToxService, because both are in separate threads. In that case to make sure the nodes
* are bootstrapped we restart the ToxService
*/
if(!DhtNode.connected)
{
if (!DhtNode.connected) {
Log.d(TAG, "Restarting START_TOX as DhtNode.connected returned false");
Intent restart = new Intent(getApplicationContext(), ToxDoService.class);
restart.setAction(Constants.START_TOX);
getApplicationContext().startService(restart);
}

/* Restart intent if it was connected before nodes were sorted */
if(DhtNode.connected && !DhtNode.sorted) {
if (DhtNode.connected && !DhtNode.sorted) {
Log.d(TAG, "Restarting START_TOX as DhtNode.sorted was false");
Intent restart = new Intent(getApplicationContext(), ToxDoService.class);
restart.setAction(Constants.START_TOX);
Expand All @@ -671,28 +672,29 @@ public void onBackPressed() {
finish();
}
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==Constants.ADD_FRIEND_REQUEST_CODE && resultCode==RESULT_OK){
if (requestCode == Constants.ADD_FRIEND_REQUEST_CODE && resultCode == RESULT_OK) {
updateLeftPane();
} else if(requestCode==Constants.SENDFILE_PICKEDFRIEND_CODE && resultCode==RESULT_OK) {
Uri uri= data.getData();
} else if (requestCode == Constants.SENDFILE_PICKEDFRIEND_CODE && resultCode == RESULT_OK) {
Uri uri = data.getData();
File pickedFile = new File(uri.getPath());
MimeTypeMap mime = MimeTypeMap.getSingleton();
Log.d("file picked",""+pickedFile.getAbsolutePath() );
Log.d("file type",""+getContentResolver().getType(uri));
Log.d("file picked", "" + pickedFile.getAbsolutePath());
Log.d("file type", "" + getContentResolver().getType(uri));
}
}

private class PaneListener implements SlidingPaneLayout.PanelSlideListener {

@Override
public void onPanelClosed(View view) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(activeTitle);
MenuItem af = menu.findItem(R.id.add_friend);
MenuItemCompat.setShowAsAction(af,MenuItem.SHOW_AS_ACTION_NEVER);
MenuItem ag= menu.add(666, 100, 100, R.string.add_to_group);
ag.setIcon(R.drawable.ic_action_add_group);
MenuItemCompat.setShowAsAction(ag,MenuItem.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setShowAsAction(af, MenuItem.SHOW_AS_ACTION_NEVER);
ag.setVisible(true);
MenuItemCompat.setShowAsAction(ag, MenuItem.SHOW_AS_ACTION_ALWAYS);
ag.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Expand All @@ -703,7 +705,7 @@ public boolean onMenuItemClick(MenuItem item) {
toxSingleton.rightPaneActive = true;
System.out.println("Panel closed");
toxSingleton.leftPaneActive = false;
if(toxSingleton.activeFriendKey!=null){
if (toxSingleton.activeFriendKey != null) {
updateChat(toxSingleton.activeFriendKey);
}
clearUselessNotifications();
Expand All @@ -714,14 +716,15 @@ public void onPanelOpened(View view) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
setTitle(R.string.app_name);
MenuItem af = menu.findItem(R.id.add_friend);
MenuItemCompat.setShowAsAction(af,MenuItem.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setShowAsAction(af, MenuItem.SHOW_AS_ACTION_IF_ROOM);
// af.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
/*af.setIcon(R.drawable.ic_action_add_person);
af.setTitle(R.string.add_friend);*/
menu.removeGroup(666);
toxSingleton.rightPaneActive =false;
supportInvalidateOptionsMenu();
toxSingleton.rightPaneActive = false;
toxSingleton.leftPaneActive = true;
InputMethodManager imm = (InputMethodManager)getSystemService(
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
/* This is causing a null pointer exception */
//imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
Expand Down

0 comments on commit 0de4040

Please sign in to comment.