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

#localesObject (readonly)

Returns the value of attribute locales.



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

def locales
  @locales
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

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

Note:

This method only support string and drawable 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



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/android/resource.rb', line 221

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



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

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

#inspectObject



396
397
398
# File 'lib/android/resource.rb', line 396

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

#key(id) ⇒ Object



305
306
307
# File 'lib/android/resource.rb', line 305

def key(id)
  key_strings[id]
end

#key_id(str) ⇒ Object

Raises:



308
309
310
311
# File 'lib/android/resource.rb', line 308

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

#key_stringsObject



302
303
304
# File 'lib/android/resource.rb', line 302

def key_strings
  @key_strings.strings
end

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

Raises:



283
284
285
286
287
288
289
290
# File 'lib/android/resource.rb', line 283

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:



273
274
275
276
277
278
279
280
281
282
# File 'lib/android/resource.rb', line 273

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



242
243
# File 'lib/android/resource.rb', line 242

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



262
263
264
265
266
267
268
269
270
271
# File 'lib/android/resource.rb', line 262

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



295
296
297
# File 'lib/android/resource.rb', line 295

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

#type_id(str) ⇒ Object

Raises:



298
299
300
301
# File 'lib/android/resource.rb', line 298

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

#type_stringsObject



292
293
294
# File 'lib/android/resource.rb', line 292

def type_strings
  @type_strings.strings
end