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.



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

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



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

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



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

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

#inspectObject



387
388
389
# File 'lib/android/resource.rb', line 387

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

#key(id) ⇒ Object



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

def key(id)
  key_strings[id]
end

#key_id(str) ⇒ Object

Raises:



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

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

#key_stringsObject



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

def key_strings
  @key_strings.strings
end

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

Raises:



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

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:



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

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



240
241
# File 'lib/android/resource.rb', line 240

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



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

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



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

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

#type_id(str) ⇒ Object

Raises:



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

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

#type_stringsObject



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

def type_strings
  @type_strings.strings
end