Skip to content

Commit

Permalink
Allow per-card menu resources
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisnelson committed Dec 16, 2013
1 parent a00bd47 commit e5a75d6
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
package com.kelsonprime.cardtree.example;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.google.android.glass.app.Card;
import com.kelsonprime.cardtree.CardTreeActivity;
import com.kelsonprime.cardtree.Level;
import com.kelsonprime.cardtree.Node;
import com.kelsonprime.cardtree.Tree;

public class MainActivity extends Activity {
public class MainActivity extends CardTreeActivity {
private static final String TAG = "MainActivity";
private Tree tree;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tree = new Tree(this);

Tree tree = getTree();
Level root = tree.getRoot();
Level second = new Level(tree);

Card one = new Card(this);
one.setText("One");
one.setText("Dead End");
root.add(new Node(one.toView()));

Card two = new Card(this);
two.setText("Two");
two.setText("Submenu");
root.add(new Node(two.toView(), second));

Card a = new Card(this);
a.setText("A");
a.setText("Click Listened to");
View viewA = a.toView();
viewA.setOnClickListener(new ClickListener());
second.add(new Node(viewA));

Card b = new Card(this);
b.setText("B");
b.setText("I have a menu");
View viewB = b.toView();
viewB.setOnFocusChangeListener(new FocusListener());
second.add(new Node(viewB));

second.add(new Node(viewB, R.menu.sample));

tree.showRoot();
setContentView(tree);
}

/**
* Kind of dirty hack to allow swipe down to go back.
*/
@Override
public void onBackPressed() {
Log.d(TAG, "Back pressed");
if(tree.isRootCurrent()){
super.onBackPressed();
}else{
tree.back();
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection. Menu items typically start another
// activity, start a service, or broadcast another intent.
switch (item.getItemId()) {
case R.id.exit:
finish();
default:
return super.onOptionsItemSelected(item);
}
}

Expand Down
9 changes: 0 additions & 9 deletions Example/src/main/res/menu/main.xml

This file was deleted.

7 changes: 7 additions & 0 deletions Example/src/main/res/menu/sample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/exit"
android:title="Exit" />
</menu>
53 changes: 53 additions & 0 deletions Lib/src/main/java/com/kelsonprime/cardtree/CardTreeActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.kelsonprime.cardtree;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

/**
* Created by kurt on 12/16/13.
*/
public class CardTreeActivity extends Activity {
private String TAG = "CardTreeActivity";
private Tree tree;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tree = new Tree(this);
}

protected Tree getTree(){
return tree;
}

/**
* Kind of dirty hack to allow swipe down to go back.
*/
@Override
public void onBackPressed() {
Log.d(TAG, "Back pressed");
if(tree.isRootCurrent()){
super.onBackPressed();
}else{
tree.back();
}
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(getCurrentNode().hasMenu()){
menu.clear();
Integer activeMenu = getCurrentNode().getMenuRes();
getMenuInflater().inflate(activeMenu, menu);
return true;
}
return false;
}

private Node getCurrentNode(){
return getTree().getCurrentNode();
}
}
11 changes: 7 additions & 4 deletions Lib/src/main/java/com/kelsonprime/cardtree/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ void click(int pos){
click(this.get(pos));
}

private void click(Node v){
if(v.hasChild()){
parent.enterLevel(v.getChild());
private void click(Node node){
if(node.hasChild()){
parent.enterLevel(node.getChild());
}else if(node.hasMenu()){
parent.showMenu();
}else{
node.click();
}
v.click();
}

void focus(int pos, boolean hasFocus){
Expand Down
24 changes: 20 additions & 4 deletions Lib/src/main/java/com/kelsonprime/cardtree/Node.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
package com.kelsonprime.cardtree;

import android.view.Menu;
import android.view.View;

public class Node {
private static final String TAG = "Node";
private final Level child;
private final View view;
private final Integer menuRes;

public Node(View view) {
this.child = null;
this.view = view;
view.setFocusable(true);
view.clearFocus();
this(view, null, null);
}

public Node(View view, Integer menuRes) {
this(view, null, menuRes);
}

public Node(View view, Level child){
this(view, child, null);
}

private Node(View view, Level child, Integer menuRes){
this.child = child;
this.view = view;
this.menuRes = menuRes;
view.setFocusable(true);
view.clearFocus();
}

public boolean hasChild() {
return child != null;
}

public boolean hasMenu() {
return menuRes != null;
}

public Level getChild(){
return child;
}
Expand All @@ -31,6 +45,8 @@ public View getView(){
return view;
}

public int getMenuRes() { return menuRes; }

void click(){
view.performClick();
}
Expand Down
20 changes: 17 additions & 3 deletions Lib/src/main/java/com/kelsonprime/cardtree/Tree.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.kelsonprime.cardtree;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;

Expand All @@ -15,9 +17,11 @@ public class Tree extends CardScrollView {
private Level root;
private Level currentLevel;
private Stack<Level> backStack;
private Activity activity;

public Tree(Context context) {
super(context);
public Tree(Activity activity) {
super(activity);
this.activity = activity;
this.backStack = new Stack<Level>();
this.root = new Level(this);
this.currentLevel = root;
Expand Down Expand Up @@ -66,12 +70,22 @@ public void back() {

private void setLevel(Level level) {
this.currentLevel = level;

adapter.setCurrentLevel(level);
setSelection(0);
this.updateViews(true);
}

// DON'T LOOK UNDER THIS LINE, I NEED TO MAKE THIS MESSAGE PASSING STUFF GO AWAY

/**
* Really gross way of doing this, I don't want to have to track the activity just for this.
*/
void showMenu() {
activity.openOptionsMenu();
}

Node getCurrentNode() { return currentLevel.get(getSelectedItemPosition());}

@Override
protected void onUnsettled(int position) {
super.onUnsettled(position);
Expand Down
Binary file removed Lib/src/main/res/drawable-hdpi/ic_launcher.png
Binary file not shown.
Binary file removed Lib/src/main/res/drawable-mdpi/ic_launcher.png
Binary file not shown.
Binary file removed Lib/src/main/res/drawable-xhdpi/ic_launcher.png
Binary file not shown.
Binary file removed Lib/src/main/res/drawable-xxhdpi/ic_launcher.png
Binary file not shown.
3 changes: 0 additions & 3 deletions Lib/src/main/res/values/strings.xml

This file was deleted.

0 comments on commit e5a75d6

Please sign in to comment.