Class: AppInfo::APK

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Helper::HumanFileSize
Defined in:
lib/app_info/apk.rb

Overview

Parse APK file

Defined Under Namespace

Modules: Device Classes: Certificate, Sign

Constant Summary

Constants included from Helper::HumanFileSize

Helper::HumanFileSize::FILE_SIZE_UNITS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::HumanFileSize

#file_to_human_size, #number_to_human_size

Constructor Details

#initialize(file) ⇒ APK

Returns a new instance of APK.



24
25
26
# File 'lib/app_info/apk.rb', line 24

def initialize(file)
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/app_info/apk.rb', line 13

def file
  @file
end

Instance Method Details

#activitiesObject



113
114
115
# File 'lib/app_info/apk.rb', line 113

def activities
  components.select { |c| c.type == 'activity' }
end

#apkObject



117
118
119
# File 'lib/app_info/apk.rb', line 117

def apk
  @apk ||= ::Android::Apk.new(@file)
end

#automotive?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/app_info/apk.rb', line 80

def automotive?
  use_features.include?('android.hardware.type.automotive')
end

#certificatesObject



107
108
109
110
111
# File 'lib/app_info/apk.rb', line 107

def certificates
  apk.certificates.each_with_object([]) do |(path, certificate), obj|
    obj << Certificate.new(path, certificate)
  end
end

#clear!Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/app_info/apk.rb', line 137

def clear!
  return unless @contents

  FileUtils.rm_rf(@contents)

  @apk = nil
  @contents = nil
  @icons = nil
  @app_path = nil
  @info = nil
end

#contentsObject



149
150
151
# File 'lib/app_info/apk.rb', line 149

def contents
  @contents ||= File.join(Dir.mktmpdir, "AppInfo-android-#{SecureRandom.hex}")
end

#device_typeObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/app_info/apk.rb', line 56

def device_type
  if wear?
    Device::WATCH
  elsif tv?
    Device::TV
  elsif automotive?
    Device::AUTOMOTIVE
  else
    Device::PHONE
  end
end

#iconsObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/app_info/apk.rb', line 121

def icons
  @icons ||= apk.icon.each_with_object([]) do |(path, data), obj|
    icon_name = File.basename(path)
    icon_path = File.join(contents, File.dirname(path))
    icon_file = File.join(icon_path, icon_name)
    FileUtils.mkdir_p icon_path
    File.write(icon_file, data, encoding: Encoding::BINARY)

    obj << {
      name: icon_name,
      file: icon_file,
      dimensions: ImageSize.path(icon_file).size
    }
  end
end

#min_sdk_versionObject Also known as: min_os_version



84
85
86
# File 'lib/app_info/apk.rb', line 84

def min_sdk_version
  manifest.min_sdk_ver
end

#nameObject



52
53
54
# File 'lib/app_info/apk.rb', line 52

def name
  manifest.label || resource.find('@string/app_name')
end

#osObject Also known as: file_type



32
33
34
# File 'lib/app_info/apk.rb', line 32

def os
  Platform::ANDROID
end

#sign_versionObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/app_info/apk.rb', line 89

def sign_version
  return 'v1' unless signs.empty?

  # when ?
  # https://source.android.com/security/apksigning/v2?hl=zh-cn
  #   'v2'
  # when ?
  # https://source.android.com/security/apksigning/v3?hl=zh-cn
  #   'v3'
  'unknown'
end

#signsObject



101
102
103
104
105
# File 'lib/app_info/apk.rb', line 101

def signs
  apk.signs.each_with_object([]) do |(path, sign), obj|
    obj << Sign.new(path, sign)
  end
end

#size(human_size: false) ⇒ Object



28
29
30
# File 'lib/app_info/apk.rb', line 28

def size(human_size: false)
  file_to_human_size(@file, human_size: human_size)
end

#tv?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/app_info/apk.rb', line 76

def tv?
  use_features.include?('android.software.leanback')
end

#version_codeObject Also known as: build_version



47
48
49
# File 'lib/app_info/apk.rb', line 47

def version_code
  manifest.version_code.to_s
end

#wear?Boolean

TODO: find a way to detect, no way! def tablet? end

Returns:

  • (Boolean)


72
73
74
# File 'lib/app_info/apk.rb', line 72

def wear?
  use_features.include?('android.hardware.type.watch')
end