33import android .app .Activity ;
44import android .content .Context ;
55import android .os .Bundle ;
6+ import android .util .Log ;
67
78import androidx .appcompat .app .AppCompatActivity ;
89
@@ -66,51 +67,17 @@ private void clearReferences () {
6667
6768 protected Boolean addAttachment () {
6869 File f = null ;
70+ String fileName = "tmp" + UUID .randomUUID ();
6971 boolean slowProfiling = BuildConfig .SLOW_PROFILING ;
70- int maxTries = 1000000 ;
72+
7173 try {
7274 Context c = getApplicationContext ();
7375 File cacheDirectory = c .getCacheDir ();
7476
7577 if (slowProfiling ) {
76- String fileName = "tmp" + UUID .randomUUID ();
77- boolean cacheFileExists = false ;
78-
79- boolean outOfBounds = false ;
80- List <Integer > indexes = new ArrayList <>();
81- int count = 0 ;
82- Random rand = new Random ();
83- File [] cacheFiles = cacheDirectory .listFiles ();
84-
85- // Loop through cache dir and check tmp file does not exist already
86- while (!outOfBounds && cacheFiles != null ) {
87- int index = rand .nextInt ();
88- int iteration = 0 ;
89- while (indexes .contains (index ) || index > cacheFiles .length || index < 0 ) {
90- index = rand .nextInt ();
91- iteration ++;
92- if (iteration > maxTries ) {
93- index = rand .nextInt (cacheFiles .length );
94- }
95- }
96-
97- if (cacheFiles [index ].getName ().equals (fileName )) {
98- cacheFileExists = true ;
99- }
100-
101- if (count == cacheFiles .length - 1 ) {
102- outOfBounds = true ;
103- }
104-
105- indexes .add (index );
106- count = count + 1 ;
107- }
108-
109- if (!cacheFileExists ) {
110- f = new File (cacheDirectory + fileName );
111- }
78+ f = generateSlowProfile (cacheDirectory , fileName );
11279 } else {
113- f = File .createTempFile ("tmp" , ".txt" , cacheDirectory );
80+ f = File .createTempFile (fileName , ".txt" , cacheDirectory );
11481 }
11582
11683 System .out .println ("File path: " +f .getAbsolutePath ());
@@ -135,4 +102,64 @@ protected Boolean addAttachment() {
135102 }
136103 return true ;
137104 }
105+
106+ protected void generateCacheFiles (int filesToGenerate , File cacheDirectory ) {
107+ for (int x = 0 ; x < filesToGenerate ; x ++) {
108+ try {
109+ File .createTempFile ("tmp" + x , ".txt" , cacheDirectory );
110+ } catch (Exception e ) {
111+ Sentry .captureException (e );
112+ e .printStackTrace ();
113+ }
114+ }
115+ }
116+
117+ protected File generateSlowProfile (File cacheDirectory , String fileName ){
118+ int maxTries = 1000000 ;
119+ boolean cacheFileExists = false ;
120+ boolean outOfBounds = false ;
121+ List <Integer > indexes = new ArrayList <>();
122+ int count = 0 ;
123+ Random rand = new Random ();
124+ File [] cacheFiles = cacheDirectory .listFiles ();
125+ File f = null ;
126+
127+ // If this is the first time the app is running or the cache has been cleared, the cacheFile length will be 1
128+ if (cacheFiles == null || cacheFiles .length <= 1 ) {
129+ generateCacheFiles (50 , cacheDirectory );
130+ cacheFiles = cacheDirectory .listFiles ();
131+ }
132+
133+ // Loop through cache dir and check tmp file does not exist already
134+ while (!outOfBounds && cacheFiles != null ) {
135+ int index = rand .nextInt ();
136+ int iteration = 0 ;
137+
138+ // Play a guess game and try to find the index for an existing file in the cache dir
139+ while (indexes .contains (index ) || index > cacheFiles .length || index < 0 ) {
140+ index = rand .nextInt ();
141+ iteration ++;
142+ if (iteration > maxTries ) {
143+ index = rand .nextInt (cacheFiles .length );
144+ }
145+ }
146+
147+ if (cacheFiles [index ].getName ().equals (fileName )) {
148+ cacheFileExists = true ;
149+ }
150+
151+ if (count == cacheFiles .length - 1 ) {
152+ outOfBounds = true ;
153+ }
154+
155+ indexes .add (index );
156+ count = count + 1 ;
157+ }
158+
159+ if (!cacheFileExists ) {
160+ f = new File (cacheDirectory + fileName );
161+ }
162+
163+ return f ;
164+ }
138165}
0 commit comments