root / trunk / data / iphone-java / HelloJava.app / HelloJava.java

Revision 380, 4.4 kB (checked in by saurik, 6 months ago)

Upgraded iPhone/Java to fully work on 2.0.

Line 
1import obc.*;
2import joc.*;
3
4import joc.Message;
5
6import static joc.Pointer.*;
7import static joc.Static.*;
8
9import java.util.ArrayList;
10import java.util.Date;
11
12public class HelloJava
13    extends UIApplication
14{
15
16private UIWindow window;
17
18private static class Contact {
19    public String first;
20    public String last;
21
22    public Contact(String first, String last) {
23        this.first = first;
24        this.last = last;
25    }
26
27    public String getName() {
28        String name = first;
29        if (last != null)
30            name += " " + last;
31        return name;
32    }
33}
34
35public static class Section {
36    public int row;
37    public String title;
38
39    public Section(int row, String title) {
40        this.row = row;
41        this.title = title;
42    }
43}
44
45private ArrayList<Contact> contacts_;
46private ArrayList<Section> sections_;
47
48@Message int numberOfSectionsInSectionList$(UISectionList list) {
49    return sections_.size();
50}
51
52@Message String sectionList$titleForSection$(UISectionList list, int section) {
53    return sections_.get(section).title;
54}
55
56@Message int sectionList$rowForSection$(UISectionList list, int section) {
57    return sections_.get(section).row;
58}
59
60@Message int numberOfRowsInTable$(UITable table) {
61    return contacts_.size();
62}
63
64@Message UITableCell table$cellForRow$column$reusing$(UITable table, int row, UITableColumn col, UITableCell reusing) {
65    Contact contact = contacts_.get(row);
66
67    UIImageAndTextTableCell cell;
68    if (reusing != null)
69        cell = (UIImageAndTextTableCell) reusing;
70    else
71        cell = (UIImageAndTextTableCell) new UIImageAndTextTableCell().init();
72
73    cell.setTitle$(contact.getName());
74    return cell;
75}
76
77@Message public byte table$canSelectRow$(UITable table, int row) {
78    return NO;
79}
80
81@Message public void applicationDidFinishLaunching$(Object unused)
82    throws Exception
83{
84    System.out.println("work");
85    contacts_ = new ArrayList<Contact>();
86    sections_ = new ArrayList<Section>();
87
88    SQLite.Database ab = new SQLite.Database();
89    ab.open(userHomeDirectory().toString() + "/Library/AddressBook/AddressBook.sqlitedb", 0666); try {
90        SQLite.Stmt st = ab.prepare("select first, last from ABPerson where first is not null order by first"); try {
91            while (st.step())
92                contacts_.add(new Contact(
93                    st.column_string(0),
94                    st.column_string(1)
95                ));
96        } finally { st.close(); }
97    } finally { ab.close(); }
98
99    CGRect outer = UIHardware.$fullScreenApplicationContentRect();
100    window = new UIWindow().initWithContentRect$(outer);
101
102    window.orderFront$(this);
103    window.makeKey$(this);
104    window._setHidden$(NO);
105
106    CGRect inner = window.bounds();
107    CGSize navsize = UINavigationBar.$defaultSize();
108    CGRect navrect = new CGRect(0, 0, inner.size.width, navsize.height);
109
110    UIView view = new UIView().initWithFrame$(inner);
111    window.setContentView$(view);
112
113    UINavigationBar navbar = new UINavigationBar().initWithFrame$(navrect);
114    view.addSubview$(navbar);
115
116    navbar.setBarStyle$(1);
117
118    UINavigationItem navitem = new UINavigationItem().initWithTitle$("Contact List");
119    navbar.pushNavigationItem$(navitem);
120
121    char letter = 0;
122    for (int i = 0; i != contacts_.size(); ++i) {
123        String name = contacts_.get(i).getName();
124        char now = name.charAt(0);
125        if (letter != now) {
126            letter = now;
127            sections_.add(new Section(
128                i,
129                new String(new char[] {now})
130            ));
131        }
132    }
133
134    CGRect lower = new CGRect(0, navsize.height, inner.size.width, inner.size.height - navsize.height);
135    UISectionList list = new UISectionList().initWithFrame$(lower);
136    view.addSubview$(list);
137
138    UITableColumn col = new UITableColumn().initWithTitle$identifier$width$("Name", "name", 320);
139
140    UITable table = (UITable) list.table();
141    table.setSeparatorStyle$(1);
142    table.addTableColumn$(col);
143    table.setReusesTableCells$(YES);
144
145    list.setDataSource$(this);
146    list.reloadData();
147
148    /* // XXX: this doesn't work the same on 2.0 and I don't want to think about porting it right now
149    AVController controller = new AVController().init();
150    CharSequence wavfile = (CharSequence) ((NSBundle) NSBundle.mainBundle()).pathForResource$ofType$("start", "wav");
151    AVItem wavitem = new AVItem().initWithPath$error$(wavfile, null);
152    wavitem.setVolume$(100);
153    controller.setCurrentItem$(wavitem);
154    controller.setCurrentTime$(0);
155    controller.play$(null);*/
156}
157
158}
Note: See TracBrowser for help on using the browser.