Android Open Source - HistoryCleanerPro Cleaner






From Project

Back to project page HistoryCleanerPro.

License

The source code is released under:

Copyright (c) 2014, John Phillips All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ...

If you think the Android project HistoryCleanerPro listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ayros.historycleaner.cleaning;
/* w  w  w  . j  av  a  2  s . co  m*/
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

import com.ayros.historycleaner.UIRunner;
import com.ayros.historycleaner.helpers.Logger;
import com.stericson.RootTools.execution.Shell;

public class Cleaner
{
  public class CleanResults
  {
    public List<String> success = new ArrayList<String>();
    public List<String> failure = new ArrayList<String>();
    
    @Override
    public String toString()
    {
      StringBuilder sb = new StringBuilder();
      
      if (failure.size() == 0)
      {
        sb.append("Cleaned all " + success.size() + " items successfully!");
      }
      else
      {
        sb.append("Cleaned " + success.size() + " items successfully\n");
        sb.append("Encountered errors with " + failure.size() + " items:");
        for (String itemName : failure)
        {
          sb.append("\n" + itemName);
        }
      }
      
      return sb.toString();
    }
  }
  
  public class CleanProgressEvent
  {
    public CleanItem item = null;
    public int itemIndex = 0;
    public int totalItems = 0;
    
    public CleanProgressEvent(CleanItem item, int itemIndex, int totalItems)
    {
      this.item = item;
      this.itemIndex = itemIndex;
      this.totalItems = totalItems;
    }
  }
  
  private List<CleanItem> cleanList;
  private boolean cleanResult;
  
  public Cleaner(List<CleanItem> itemList)
  {
    cleanList = new ArrayList<CleanItem>(itemList);
  }
  
  /**
   * Cleans all items asynchronously. If an activity is provided, all cleaning
   * and events will be ran on its UI thread. Also, if a root shell is
   * required, it must already be running.
   * 
   * @param cl
   * @return
   */
  public void cleanAsync(final Activity activity, final CleanListener cl)
  {
    new Thread()
    {
      @Override
      public void run()
      {
        CleanResults results = new CleanResults();
        
        if (isRootRequired() && !Shell.isRootShellOpen())
        {
          Logger.errorST("Root shell isn't started, cannot clean items");
          
          for (CleanItem item : cleanList)
          {
            results.failure.add(item.getUniqueName());
          }
        }
        else
        // If root shell is needed, it is started
        {
          for (int i = 0; i < cleanList.size(); i++)
          {
            CleanItem item = cleanList.get(i);
            
            Logger.debug("About to clean item: " + item.getUniqueName());
            
            // Send progressChanged message
            if (cl != null)
            {
              CleanProgressEvent cpe = new CleanProgressEvent(item, i, cleanList.size());
              
              // If activity is provided, run event on UI thread
              if (activity != null)
              {
                new UIRunner(activity, cpe)
                {
                  @Override
                  public void action(Object data)
                  {
                    cl.progressChanged((CleanProgressEvent)data);
                  }
                }.runAndWait();
              }
              else
              {
                cl.progressChanged(cpe);
              }
            }
            
            // Clean item running on UI thread if necessary
            cleanResult = false;
            if (item.runOnUIThread() && activity != null)
            {
              new UIRunner(activity, item)
              {
                @Override
                public void action(Object data)
                {
                  try
                  {
                    Cleaner.this.cleanResult = ((CleanItem)data).clean();
                  }
                  catch (Exception e)
                  {
                    Logger.errorST("Exception cleaning item " + ((CleanItem)data).getUniqueName(), e);
                    Cleaner.this.cleanResult = false;
                  }
                }
              }.runAndWait();
            }
            else
            {
              try
              {
                cleanResult = item.clean();
              }
              catch (Exception e)
              {
                Logger.errorST("Exception cleaning item " + item.getUniqueName(), e);
                cleanResult = false;
              }
            }
            item.postClean();
            
            // Add result to clean results
            if (cleanResult)
            {
              Logger.debug("Cleaned Successfully: " + item.getUniqueName());
              results.success.add(item.getUniqueName());
            }
            else
            {
              Logger.debug("Cleaning failure: " + item.getUniqueName());
              results.failure.add(item.getUniqueName());
            }
          }
        }
        
        // Send cleaningComplete event
        if (cl != null)
        {
          // If activity is provided, run event on UI thread
          if (activity != null)
          {
            new UIRunner(activity, results)
            {
              @Override
              public void action(Object data)
              {
                cl.cleaningComplete((CleanResults)data);
              }
            }.runAndWait();
          }
          else
          {
            cl.cleaningComplete(results);
          }
        }
      }
    }.start();
  }
  
  /**
   * Cleans all items and sends all events on the current thread. Assumes
   * current thread is a UI thread if it is needed for any items. Also, if a
   * root shell is required, it must already be running.
   * 
   * @param cl
   * @return
   */
  public CleanResults cleanNow(CleanListener cl)
  {
    CleanResults results = new CleanResults();
    
    if (isRootRequired() && !Shell.isRootShellOpen())
    {
      Logger.errorST("Root shell isn't started, cannot clean items");
      
      for (CleanItem item : cleanList)
      {
        results.failure.add(item.getUniqueName());
      }
    }
    else
    {
      for (int i = 0; i < cleanList.size(); i++)
      {
        CleanItem item = cleanList.get(i);
        
        if (cl != null)
        {
          cl.progressChanged(new CleanProgressEvent(item, i, cleanList.size()));
        }
        
        try
        {
          if (item.clean())
          {
            results.success.add(item.getUniqueName());
          }
          else
          {
            results.failure.add(item.getUniqueName());
          }
        }
        catch (Exception e)
        {
          Logger.errorST("Exception cleaning item " + item.getUniqueName(), e);
          results.failure.add(item.getUniqueName());
        }
        
        item.postClean();
      }
    }
    
    if (cl != null)
    {
      cl.cleaningComplete(results);
    }
    
    return results;
  }
  
  public boolean isRootRequired()
  {
    for (CleanItem item : cleanList)
    {
      if (item.isRootRequired())
      {
        return true;
      }
    }
    
    return false;
  }
}




Java Source Code List

com.ayros.historycleaner.Globals.java
com.ayros.historycleaner.ProfileAdapter.java
com.ayros.historycleaner.UIRunner.java
com.ayros.historycleaner.cleaning.CategoryList.java
com.ayros.historycleaner.cleaning.Category.java
com.ayros.historycleaner.cleaning.CleanItem.java
com.ayros.historycleaner.cleaning.CleanListener.java
com.ayros.historycleaner.cleaning.Cleaner.java
com.ayros.historycleaner.cleaning.ProfileList.java
com.ayros.historycleaner.cleaning.Profile.java
com.ayros.historycleaner.cleaning.SimpleDatabaseItem.java
com.ayros.historycleaner.cleaning.SimpleFileItem.java
com.ayros.historycleaner.cleaning.items._AdobeReader_Recent.java
com.ayros.historycleaner.cleaning.items._FirefoxBeta_Cache.java
com.ayros.historycleaner.cleaning.items._FirefoxBeta_Cookies.java
com.ayros.historycleaner.cleaning.items._FirefoxBeta_History.java
com.ayros.historycleaner.cleaning.items._FirefoxBeta_LocalStorage.java
com.ayros.historycleaner.cleaning.items._FirefoxBeta_OpenTabs.java
com.ayros.historycleaner.cleaning.items._Firefox_Cache.java
com.ayros.historycleaner.cleaning.items._Firefox_Cookies.java
com.ayros.historycleaner.cleaning.items._Firefox_History.java
com.ayros.historycleaner.cleaning.items._Firefox_LocalStorage.java
com.ayros.historycleaner.cleaning.items._Firefox_OpenTabs.java
com.ayros.historycleaner.cleaning.items._ONEBrowser_Cache.java
com.ayros.historycleaner.cleaning.items._ONEBrowser_LocalStorage.java
com.ayros.historycleaner.cleaning.items._System_BrowserHistory.java
com.ayros.historycleaner.cleaning.items._System_Cache.java
com.ayros.historycleaner.cleaning.items._System_Clipboard.java
com.ayros.historycleaner.cleaning.items._System_FrequentContacts.java
com.ayros.historycleaner.cleaning.items._System_RecentCalls.java
com.ayros.historycleaner.cleaning.items._System_SMS.java
com.ayros.historycleaner.helpers.DBHelper.java
com.ayros.historycleaner.helpers.DatabaseModifier.java
com.ayros.historycleaner.helpers.Helper.java
com.ayros.historycleaner.helpers.Logger.java
com.ayros.historycleaner.helpers.PrefsModifier.java
com.ayros.historycleaner.helpers.RootHelper.java
com.ayros.historycleaner.helpers.XML.java
com.ayros.historycleaner.locale.Constants.java
com.ayros.historycleaner.locale.bundle.BundleScrubber.java
com.ayros.historycleaner.locale.bundle.PluginBundleManager.java
com.ayros.historycleaner.locale.receiver.FireReceiver.java
com.ayros.historycleaner.locale.ui.AbstractPluginActivity.java
com.ayros.historycleaner.locale.ui.EditPluginActivity.java
com.ayros.historycleaner.ui.CleanFragment.java
com.ayros.historycleaner.ui.DataViewActivity.java
com.ayros.historycleaner.ui.HelpActivity.java
com.ayros.historycleaner.ui.MainActivity.java
com.ayros.historycleaner.ui.OnProfileUpdated.java
com.ayros.historycleaner.ui.ProfileFragment.java
com.ayros.historycleaner.ui.ShortcutActivity.java
com.ayros.historycleaner.ui.ShortcutCleanActivity.java
com.twofortyfouram.locale.BreadCrumber.java
com.twofortyfouram.locale.Constants.java
com.twofortyfouram.locale.Intent.java
com.twofortyfouram.locale.PackageUtilities.java