Class: Android::Resource::ResTablePackage

Inherits:
ChunkHeader show all
Defined in:
lib/android/resource.rb

Instance Attribute Summary collapse

Attributes inherited from ChunkHeader

#header_size, #size

Instance Method Summary collapse

Methods inherited from Chunk

#current_position, #exec_parse, #initialize, #read_int16, #read_int32, #read_int8

Constructor Details

This class inherits a constructor from Android::Resource::Chunk

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



112
113
114
# File 'lib/android/resource.rb', line 112

def name
  @name
end

Instance Method Details

#find(res_id, opts = {}) ⇒ Object

Note:

This method only support string and drawable/mipmap resource for now.

Note:

Always return nil if assign not string type res id.

find resource by resource id

Parameters:

  • res_id (String)

    (like ‘@0x7f010001’ or ‘@string/key’)

  • opts (Hash) (defaults to: {})

    option

Options Hash (opts):

  • :lang (String)

    language code like ‘ja’, ‘cn’…

  • :contry (String)

    cantry code like ‘jp’…

Raises:

  • (ArgumentError)

    invalid id format



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/android/resource.rb', line 130

def find(res_id, opts={})
  hex_id = strid2int(res_id)
  tid = ((hex_id&0xff0000) >>16)
  key = hex_id&0xffff

  case type(tid) 
  when 'string'
    return find_res_string(key, opts)
  when 'drawable', 'mipmap'
    drawables = []
    @types[tid].each do |type|
      unless type[key].nil?
        drawables << @global_string_pool.strings[type[key].val.data]
      end
    end
    return drawables
  else
    nil
  end
end

#global_string_pool=(pool) ⇒ Object



114
115
116
117
# File 'lib/android/resource.rb', line 114

def global_string_pool=(pool)
  @global_string_pool = pool
  extract_res_strings
end

#inspectObject



289
290
291
# File 'lib/android/resource.rb', line 289

def inspect
  "<ResTablePackage offset:%#08x, size:%#x, name:\"%s\">" % [@offset, @size, @name]
end

#key(id) ⇒ Object



213
214
215
# File 'lib/android/resource.rb', line 213

def key(id)
  key_strings[id]
end

#key_id(str) ⇒ Object

Raises:



216
217
218
219
# File 'lib/android/resource.rb', line 216

def key_id(str)
  raise NotFoundError unless key_strings.include? str
  key_strings.index(str) 
end

#key_stringsObject



210
211
212
# File 'lib/android/resource.rb', line 210

def key_strings
  @key_strings.strings
end

#res_hex_id(readable_id, opt = {}) ⇒ Object

Raises:



191
192
193
194
195
196
197
198
# File 'lib/android/resource.rb', line 191

def res_hex_id(readable_id, opt={})
  dummy, typestr, keystr = readable_id.match(/^@?(\w+)\/(\w+)$/).to_a
  tid = type_id(typestr)
  raise NotFoundError unless @types.has_key?(tid)
  keyid = @types[tid][0].keys[keystr]
  raise NotFoundError if keyid.nil?
  "@0x7f%02x%04x" % [tid, keyid]
end

#res_readable_id(hex_id) ⇒ Object

Raises:



181
182
183
184
185
186
187
188
189
190
# File 'lib/android/resource.rb', line 181

def res_readable_id(hex_id)
  if hex_id.kind_of? String
    hex_id = hex_id.sub(/^@/,'').to_i(16)
  end
  tid = ((hex_id&0xff0000) >>16)
  key = hex_id&0xffff
  raise NotFoundError if !@types.has_key?(tid) || @types[tid][0][key].nil?
  keyid= @types[tid][0][key].key # ugh!
  "@#{type(tid)}/#{key(keyid)}"
end

#res_typesObject



151
152
# File 'lib/android/resource.rb', line 151

def res_types
end

#strid2int(res_id) ⇒ Fixnum

convert string resource id to fixnum

Parameters:

  • res_id (String)

    (like ‘@0x7f010001’ or ‘@string/key’)

Returns:

  • (Fixnum)

    integer id (like 0x7f010001)

Raises:

  • (ArgumentError)

    invalid format



170
171
172
173
174
175
176
177
178
179
# File 'lib/android/resource.rb', line 170

def strid2int(res_id)
  case res_id
  when /^@?0x[0-9a-fA-F]{8}$/
    return res_id.sub(/^@/,'').to_i(16)
  when /^@?\w+\/\w+/
    return res_hex_id(res_id).sub(/^@/,'').to_i(16)
  else
    raise ArgumentError
  end
end

#type(id) ⇒ Object



203
204
205
# File 'lib/android/resource.rb', line 203

def type(id)
  type_strings[id-1]
end

#type_id(str) ⇒ Object

Raises:



206
207
208
209
# File 'lib/android/resource.rb', line 206

def type_id(str)
  raise NotFoundError unless type_strings.include? str
  type_strings.index(str) + 1
end

#type_stringsObject



200
201
202
# File 'lib/android/resource.rb', line 200

def type_strings
  @type_strings.strings
end