|
| 1 | +ruboto_import "#{$callbacks_package}.RubotoRunnable" |
| 2 | +ruboto_import "#{$callbacks_package}.RubotoDialogOnClickListener" |
| 3 | + |
| 4 | +java_import "android.os.Message" |
| 5 | +java_import "android.os.Handler" |
| 6 | +java_import "android.app.ProgressDialog" |
| 7 | +java_import "java.net.URL" |
| 8 | +java_import "java.io.BufferedInputStream" |
| 9 | +java_import "java.util.zip.GZIPInputStream" |
| 10 | + |
| 11 | +class Activity |
| 12 | + def alert_dialog(options) |
| 13 | + dialog = Java::android.app::AlertDialog::Builder.new(self) |
| 14 | + options.each do |k, v| |
| 15 | + if v.is_a?(Array) |
| 16 | + if v[-1].is_a?(Proc) |
| 17 | + proc = v[-1] |
| 18 | + v[-1] = RubotoDialogOnClickListener.new.handle_click{|d, w| proc.call(d,w)} |
| 19 | + end |
| 20 | + dialog.send("set#{k.to_s.gsub(/(^|_)([a-z])/) {$2.upcase}}", *v) |
| 21 | + else |
| 22 | + dialog.send("set#{k.to_s.gsub(/(^|_)([a-z])/) {$2.upcase}}", v) |
| 23 | + end |
| 24 | + end |
| 25 | + dialog.create.show |
| 26 | + end |
| 27 | + |
| 28 | + def launch_list(a_title, query_or_array, &block) |
| 29 | + self.start_ruboto_activity "$list" do |
| 30 | + setTitle a_title |
| 31 | + |
| 32 | + setup_content do |
| 33 | + if query_or_array.is_a?(Array) |
| 34 | + @lv = list_view :list => query_or_array |
| 35 | + else |
| 36 | + if query_or_array.is_a?(String) |
| 37 | + @cursor = $db.rawQuery(query_or_array, nil) |
| 38 | + startManagingCursor(@cursor) |
| 39 | + else |
| 40 | + @cursor = query_or_array |
| 41 | + end |
| 42 | + |
| 43 | + setTitle "#{a_title} (#{@cursor.getCount})" |
| 44 | + |
| 45 | + merge_adapter = MergeAdapter.new |
| 46 | + |
| 47 | + merge_adapter.addView( |
| 48 | + text_view(:text => "Limiting view (API #{SQLiteModel.get_api_limit})", |
| 49 | + :gravity => Gravity::CENTER_HORIZONTAL, |
| 50 | + :text_size => 20, |
| 51 | + :background_color => Color::WHITE, |
| 52 | + :text_color => Color::BLACK) |
| 53 | + ) unless SQLiteModel::get_api_limit.nil? |
| 54 | + |
| 55 | + adapter = SimpleCursorAdapter.new(self, Ruboto::R::layout::list_item_single, @cursor, |
| 56 | + ["name"].to_java(:string), [Ruboto::Id::text1].to_java(:int)); |
| 57 | + adapter.setViewBinder(RubotoCursorViewBinder.main) |
| 58 | + merge_adapter.addAdapter adapter |
| 59 | + |
| 60 | + @lv = list_view :adapter => merge_adapter |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + handle_item_click do |adapter_view, view, pos, item_id| |
| 65 | + block.call(self, adapter_view, view, pos, item_id) |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def progress_dialog(argh={}) |
| 71 | + pd = ProgressDialog.new(self) |
| 72 | + pd.setTitle(argh[:title]) if argh[:title] |
| 73 | + pd.setMessage(argh[:message]) if argh[:message] |
| 74 | + pd.setProgressStyle((argh[:style] and argh[:style] == :horizontal) ? |
| 75 | + ProgressDialog::STYLE_HORIZONTAL : ProgressDialog::STYLE_SPINNER) |
| 76 | + pd.setMax(argh[:max]) if argh[:max] |
| 77 | + pd.setIndeterminate argh[:indeterminate] if argh[:indeterminate] |
| 78 | + pd.show |
| 79 | + pd |
| 80 | + end |
| 81 | + |
| 82 | + def do_in_thread(argh = {}, &block) |
| 83 | + handler = Handler.new() |
| 84 | + |
| 85 | + Thread.new do |
| 86 | + begin |
| 87 | + block.call |
| 88 | + handler.post(RubotoRunnable.new.handle_run{argh[:when_done].call if argh[:when_done]}) |
| 89 | + rescue |
| 90 | + handler.post(RubotoRunnable.new.handle_run{argh[:when_failed].call if argh[:when_failed]}) |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + def download_in_thread(args) |
| 96 | + gzipped = args[:from][-3..-1] == ".gz" |
| 97 | + base_file_name = args[:from].split("/")[-1] |
| 98 | + base_file_name = base_file_name[0..-4] if gzipped |
| 99 | + |
| 100 | + progress_dialog(:style => :horizontal, :title => "Downloading...", |
| 101 | + :max => args[:size]).do_in_thread(args[:when_done], args[:when_failed]) do |
| 102 | + args[:when_starting].call if args[:when_starting] |
| 103 | + f = nil |
| 104 | + |
| 105 | + begin |
| 106 | + connection = URL.new(args[:from]).openConnection |
| 107 | + FileUtils.makedirs args[:to] unless File.directory? args[:to] |
| 108 | + f = File.open("#{args[:to]}/#{base_file_name}", "w") |
| 109 | + buff_size = 0x2000 |
| 110 | + buff = Java::byte[buff_size].new |
| 111 | + |
| 112 | + i = connection.getInputStream |
| 113 | + i = GZIPInputStream.new(i) if gzipped |
| 114 | + i = BufferedInputStream.new(i, buff_size) |
| 115 | + x = i.read(buff, 0, buff_size) |
| 116 | + |
| 117 | + while x != -1 |
| 118 | + f << String.from_java_bytes(x == buff_size ? buff : buff[0..(x-1)]) |
| 119 | + $progress_dialog.increment_progress(x) |
| 120 | + x = i.read(buff, 0, buff_size) |
| 121 | + end |
| 122 | + $progress_dialog.success = true |
| 123 | + rescue |
| 124 | + $progress_dialog.success = false |
| 125 | + FileUtils.rm "#{args[:to]}/#{base_file_name}" if f and File.exists?("#{args[:to]}/#{base_file_name}") |
| 126 | + ensure |
| 127 | + connection.disconnect |
| 128 | + f.close |
| 129 | + args[:when_ending].call if args[:when_ending] |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | +end |
| 134 | + |
| 135 | +class ProgressDialog |
| 136 | + attr_reader :handler |
| 137 | + attr_writer :success |
| 138 | + |
| 139 | + def success? |
| 140 | + @success.nil? ? true : @success |
| 141 | + end |
| 142 | + |
| 143 | + def increment_progress(amount) |
| 144 | + @handler.post(RubotoRunnable.new.handle_run{$progress_dialog.incrementProgressBy(amount)}) |
| 145 | + end |
| 146 | + |
| 147 | + def do_in_thread(when_done=nil, when_failed=nil, &block) |
| 148 | + $progress_dialog = self |
| 149 | + @handler = Handler.new() |
| 150 | + |
| 151 | + Thread.new do |
| 152 | + block.call |
| 153 | + |
| 154 | + $progress_dialog.handler.post(RubotoRunnable.new.handle_run do |
| 155 | + $progress_dialog.dismiss |
| 156 | + finish_with = $progress_dialog.success? ? when_done : when_failed |
| 157 | + finish_with.call if finish_with |
| 158 | + $progress_dialog = nil |
| 159 | + end) |
| 160 | + end |
| 161 | + end |
| 162 | +end |
0 commit comments