-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_prefs.rb
114 lines (98 loc) · 3.53 KB
/
shared_prefs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true
require_relative 'validate_package'
class SharedPrefs
def self.name
'shared_prefs'
end
def self.perform(*args)
subcommand = args[0]
return unless validate_sub_command(subcommand)
package_name = args[1]
return unless validate_package(package_name)
# don't validate these until we need them; they don't need to be supplied for all subcommands
file_name = args[2]
pref_name = args[3]
file_name_present = !file_name.nil? && !file_name.empty?
case subcommand
when "list"
if file_name_present
puts "Listing shared prefs content for #{package_name} / #{file_name}:\n\n"
full_file_path = "/data/data/#{package_name}/shared_prefs/#{file_name}"
return unless validate_file_exists(package_name, full_file_path, file_name)
list_contents_of_single_file = ->(stdin) {
stdin.write "run-as #{package_name}\n"
stdin.write "cat #{full_file_path}\n"
}
run_as_adb_shell(list_contents_of_single_file)
else
puts "Listing all shared prefs files for #{package_name}:\n\n"
list_all_shared_prefs_files = ->(stdin) {
stdin.write "run-as #{package_name}\n"
stdin.write "ls -al /data/data/#{package_name}/shared_prefs\n"
}
run_as_adb_shell(list_all_shared_prefs_files)
end
when "remove"
return unless validate_file_exists(package_name, full_file_path, file_name)
return unless validate_pref_name(pref_name)
puts "Removing #{pref_name} from shared prefs #{package_name} / #{file_name}"
clear_single_shared_pref = ->(stdin) {
stdin.write "run-as #{package_name}\n"
full_file_name = "/data/data/#{package_name}/shared_prefs/#{file_name}"
regex = "'/^.*name=\"#{pref_name}\".*$/d'"
stdin.write "sed -i #{regex} #{full_file_name}\n"
}
run_as_adb_shell(clear_single_shared_pref)
puts "You may need to restart application for changes to take effect."
end
end
def self.validate_pref_name(pref_name)
if pref_name.nil? || pref_name.empty?
puts 'Failed to supply preference name'
return false
end
return true
# TODO validate that pref is even there before we try to clear it
# TODO if we don't find it try to scan all of the other pref files in this package
end
def self.validate_file_exists(package_name, full_file_path, file_name)
Open3.pipeline_rw "adb shell" do |stdin, stdout, _ts|
stdin.write "run-as #{package_name}\n"
stdin.write "if [ -e #{full_file_path} ]; then echo true; else echo false; fi \n"
stdin.close
out = stdout.read # adb will return true/false as a string with a carriage returnru
if out.strip() == "false"
puts "Invalid file name: #{file_name}"
return false
else
return true
end
end
Process.waitall
end
def self.run_as_adb_shell(lambda)
Open3.pipeline_rw "adb shell" do |stdin, stdout, _ts|
# I can write to adb shell all day long
lambda.call(stdin)
stdin.write "exit\n"
stdin.close
out = stdout.read
puts out
end
Process.waitall
end
def self.validate_sub_command(subcommand)
valid_subcommands = ["list", "remove"]
unless valid_subcommands.include? subcommand
puts "Unknown subcommand: #{subcommand}. Choices are: "
valid_subcommands.each { |x|
puts " #{x}"
}
return false
end
true
end
def self.similar_sounding_commands;
%w[preferences share shared list remove delete]
end
end