Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: mobile/android/base/distribution/Distribution.java

Issue 4540363985387520: Embed Adblock Plus (Closed)
Left Patch Set: WIP - Use the mobile/android/extensions directory Created Jan. 3, 2015, 2:17 a.m.
Right Patch Set: Install as application scope extension Created Jan. 23, 2015, 11:20 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
LEFTRIGHT
(no file at all)
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public 2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 5
6 package org.mozilla.gecko.distribution; 6 package org.mozilla.gecko.distribution;
7 7
8 import java.io.BufferedInputStream; 8 import java.io.BufferedInputStream;
9 import java.io.File; 9 import java.io.File;
10 import java.io.FileNotFoundException; 10 import java.io.FileNotFoundException;
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 * set and populated. 333 * set and populated.
334 * 334 *
335 * This method is *only* protected for use from testDistribution. 335 * This method is *only* protected for use from testDistribution.
336 * 336 *
337 * @return true if we've set a distribution. 337 * @return true if we've set a distribution.
338 */ 338 */
339 @RobocopTarget 339 @RobocopTarget
340 protected boolean doInit() { 340 protected boolean doInit() {
341 ThreadUtils.assertNotOnUiThread(); 341 ThreadUtils.assertNotOnUiThread();
342 342
343 // Firefox for Android normally ignores application scope extensions,
344 // and thus doesn't put the extensions directory in place. In Adblock
345 // Browser however, we need this mechanism.
346 //
347 // This logic doesn't really belong here. However, here the change is
348 // minimally invasive, and we're more likely to notice when the logic
349 // changes for the distributions directory.
350 try {
351 copyExtensionsFiles();
352 } catch (IOException e) {
353 Log.e(LOGTAG, "Error copying extensions files from APK.", e);
354 }
355
343 // Bail if we've already tried to initialize the distribution, and 356 // Bail if we've already tried to initialize the distribution, and
344 // there wasn't one. 357 // there wasn't one.
345 final SharedPreferences settings; 358 final SharedPreferences settings;
346 if (prefsBranch == null) { 359 if (prefsBranch == null) {
347 settings = GeckoSharedPrefs.forApp(context); 360 settings = GeckoSharedPrefs.forApp(context);
348 } else { 361 } else {
349 settings = context.getSharedPreferences(prefsBranch, Activity.MODE_P RIVATE); 362 settings = context.getSharedPreferences(prefsBranch, Activity.MODE_P RIVATE);
350 } 363 }
351 364
352 String keyName = context.getPackageName() + ".distribution_state"; 365 String keyName = context.getPackageName() + ".distribution_state";
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 fileStream.close(); 656 fileStream.close();
644 } 657 }
645 } 658 }
646 } finally { 659 } finally {
647 zip.close(); 660 zip.close();
648 } 661 }
649 662
650 return distributionSet; 663 return distributionSet;
651 } 664 }
652 665
666 /**
667 * Copies the /extensions folder out of the APK and into the app's data dire ctory.
668 * Adapted from copyFiles().
669 */
670 private void copyExtensionsFiles() throws IOException {
671 final File applicationPackage = new File(packagePath);
672 final ZipFile zip = new ZipFile(applicationPackage);
673
674 try {
675 final byte[] buffer = new byte[1024];
676
677 final Enumeration<? extends ZipEntry> zipEntries = zip.entries();
678 while (zipEntries.hasMoreElements()) {
679 final ZipEntry fileEntry = zipEntries.nextElement();
680 final String name = fileEntry.getName();
681
682 if (fileEntry.isDirectory()) {
683 // We'll let getDataFile deal with creating the directory hi erarchy.
684 continue;
685 }
686
687 if (!name.startsWith("extensions/")) {
688 continue;
689 }
690
691 final File outFile = getDataFile(name);
692 if (outFile == null) {
693 continue;
694 }
695
696 final InputStream fileStream = zip.getInputStream(fileEntry);
697 try {
698 writeStream(fileStream, outFile, fileEntry.getTime(), buffer );
699 } finally {
700 fileStream.close();
701 }
702 }
703 } finally {
704 zip.close();
705 }
706 }
707
653 private void writeStream(InputStream fileStream, File outFile, final long mo difiedTime, byte[] buffer) 708 private void writeStream(InputStream fileStream, File outFile, final long mo difiedTime, byte[] buffer)
654 throws FileNotFoundException, IOException { 709 throws FileNotFoundException, IOException {
655 final OutputStream outStream = new FileOutputStream(outFile); 710 final OutputStream outStream = new FileOutputStream(outFile);
656 try { 711 try {
657 int count; 712 int count;
658 while ((count = fileStream.read(buffer)) > 0) { 713 while ((count = fileStream.read(buffer)) > 0) {
659 outStream.write(buffer, 0, count); 714 outStream.write(buffer, 0, count);
660 } 715 }
661 716
662 outFile.setLastModified(modifiedTime); 717 outFile.setLastModified(modifiedTime);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
777 } 832 }
778 833
779 /** 834 /**
780 * A safe way for callers to determine if this Distribution instance 835 * A safe way for callers to determine if this Distribution instance
781 * represents a real live distribution. 836 * represents a real live distribution.
782 */ 837 */
783 public boolean exists() { 838 public boolean exists() {
784 return state == STATE_SET; 839 return state == STATE_SET;
785 } 840 }
786 } 841 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld