diff --git a/app/src/main/java/us/shandian/giga/get/DownloadManagerImpl.java b/app/src/main/java/us/shandian/giga/get/DownloadManagerImpl.java index 4bbaccf..f1491b6 100755 --- a/app/src/main/java/us/shandian/giga/get/DownloadManagerImpl.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadManagerImpl.java @@ -20,7 +20,7 @@ public class DownloadManagerImpl implements DownloadManager private Context mContext; private String mLocation; - protected ArrayList mMissions = new ArrayList(); + private ArrayList mMissions = new ArrayList(); public DownloadManagerImpl(Context context, String location) { mContext = context; diff --git a/app/src/main/java/us/shandian/giga/get/DownloadMission.java b/app/src/main/java/us/shandian/giga/get/DownloadMission.java index 866e17b..b6b0b2b 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadMission.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadMission.java @@ -38,9 +38,9 @@ public static interface MissionListener { public long length = 0; public long done = 0; public int threadCount = 3; - public int finishCount = 0; - public ArrayList threadPositions = new ArrayList(); - public HashMap blockState = new HashMap(); + private int finishCount = 0; + private ArrayList threadPositions = new ArrayList(); + private HashMap blockState = new HashMap(); public boolean running = false; public boolean finished = false; public boolean fallback = false; diff --git a/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java b/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java index 915180b..df2c31b 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java @@ -10,7 +10,7 @@ import static us.shandian.giga.BuildConfig.DEBUG; -public class DownloadRunnable implements Runnable +class DownloadRunnable implements Runnable { private static final String TAG = DownloadRunnable.class.getSimpleName(); diff --git a/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java b/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java index 50bdce8..5262eac 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java @@ -6,7 +6,7 @@ import java.net.URL; // Single-threaded fallback mode -public class DownloadRunnableFallback implements Runnable +class DownloadRunnableFallback implements Runnable { private DownloadMission mMission; //private int mId; diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/NavigationAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/NavigationAdapter.java index 05ebb61..b959584 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/NavigationAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/NavigationAdapter.java @@ -11,78 +11,75 @@ import us.shandian.giga.R; import us.shandian.giga.util.Utility; -public class NavigationAdapter extends BaseAdapter -{ - private Context mContext; - private LayoutInflater mInflater; - private String[] mStrs; - private int[] mIcons; - - public NavigationAdapter(Context context, int strs, int icons) { - mContext = context; - mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - mStrs = context.getResources().getStringArray(strs); - - String[] iconStr = context.getResources().getStringArray(icons); - mIcons = new int[iconStr.length]; - - for (int i = 0; i < iconStr.length; i++) { - String str = iconStr[i]; - if (!str.equals("null")) { - mIcons[i] = context.getResources().getIdentifier(str, "drawable", "us.shandian.giga"); - } - } - } - - @Override - public int getCount() { - return mStrs.length; - } - - @Override - public Object getItem(int i) { - return mStrs[i]; - } - - @Override - public long getItemId(int i) { - return i; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - if (position >= getCount()) { - return null; - } else { - String str = mStrs[position]; - View v = convertView; - - if (str.equals("-")) { - v = mInflater.inflate(R.layout.drawer_divider, parent, false); - v.setTag(1); - return v; - } else { - - if (v == null || ((Integer)v.getTag()) == 1) { - v = mInflater.inflate(R.layout.drawer_item, parent, false); - v.setTag(0); - } - - ImageView iv = Utility.findViewById(v, R.id.drawer_icon); - TextView tv = Utility.findViewById(v, R.id.drawer_text); - - if (iv != null && tv != null) { - - iv.setImageDrawable(mContext.getResources().getDrawable(mIcons[position])); - tv.setText(str); - tv.getPaint().setFakeBoldText(true); - - } - - return v; - - } - } - } +public class NavigationAdapter extends BaseAdapter { + private Context mContext; + private LayoutInflater mInflater; + private String[] mStrs; + private int[] mIcons; + + public NavigationAdapter(Context context, int strs, int icons) { + mContext = context; + mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + mStrs = context.getResources().getStringArray(strs); + + String[] iconStr = context.getResources().getStringArray(icons); + mIcons = new int[iconStr.length]; + + for (int i = 0; i < iconStr.length; i++) { + String str = iconStr[i]; + if (!str.equals("null")) { + mIcons[i] = context.getResources().getIdentifier(str, "drawable", "us.shandian.giga"); + } + } + } + + @Override + public int getCount() { + return mStrs.length; + } + + @Override + public Object getItem(int i) { + return mStrs[i]; + } + + @Override + public long getItemId(int i) { + return i; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + if (position >= getCount()) { + return null; + } else { + String str = mStrs[position]; + View v = convertView; + if (str.equals("-")) { + v = mInflater.inflate(R.layout.drawer_divider, parent, false); + v.setTag(1); + return v; + } else { + + if (v == null || ((Integer) v.getTag()) == 1) { + v = mInflater.inflate(R.layout.drawer_item, parent, false); + v.setTag(0); + } + + ImageView iv = Utility.findViewById(v, R.id.drawer_icon); + TextView tv = Utility.findViewById(v, R.id.drawer_text); + + if (iv != null && tv != null ) { + iv.setImageDrawable(mContext.getResources().getDrawable(mIcons[position])); + tv.setText(str); + tv.getPaint().setFakeBoldText(true); + + } + + return v; + + } + } + } } diff --git a/app/src/main/java/us/shandian/giga/ui/common/FloatingActionButton.java b/app/src/main/java/us/shandian/giga/ui/common/FloatingActionButton.java index 424ce38..bb57844 100644 --- a/app/src/main/java/us/shandian/giga/ui/common/FloatingActionButton.java +++ b/app/src/main/java/us/shandian/giga/ui/common/FloatingActionButton.java @@ -24,14 +24,14 @@ */ public class FloatingActionButton extends View implements Animator.AnimatorListener { - final static OvershootInterpolator overshootInterpolator = new OvershootInterpolator(); - final static AccelerateInterpolator accelerateInterpolator = new AccelerateInterpolator(); + private final static OvershootInterpolator overshootInterpolator = new OvershootInterpolator(); + private final static AccelerateInterpolator accelerateInterpolator = new AccelerateInterpolator(); - Context context; - Paint mButtonPaint; - Paint mDrawablePaint; - Bitmap mBitmap; - boolean mHidden = false; + private Context context; + private Paint mButtonPaint; + private Paint mDrawablePaint; + private Bitmap mBitmap; + private boolean mHidden = false; public FloatingActionButton(Context context) { super(context); @@ -39,16 +39,16 @@ public FloatingActionButton(Context context) { init(Color.WHITE); } - public void setFloatingActionButtonColor(int FloatingActionButtonColor) { + private void setFloatingActionButtonColor(int FloatingActionButtonColor) { init(FloatingActionButtonColor); } - public void setFloatingActionButtonDrawable(Drawable FloatingActionButtonDrawable) { + private void setFloatingActionButtonDrawable(Drawable FloatingActionButtonDrawable) { mBitmap = ((BitmapDrawable) FloatingActionButtonDrawable).getBitmap(); invalidate(); } - public void init(int FloatingActionButtonColor) { + private void init(int FloatingActionButtonColor) { setWillNotDraw(false); setLayerType(View.LAYER_TYPE_SOFTWARE, null); diff --git a/app/src/main/java/us/shandian/giga/ui/common/ProgressDrawable.java b/app/src/main/java/us/shandian/giga/ui/common/ProgressDrawable.java index a375322..bf42be2 100644 --- a/app/src/main/java/us/shandian/giga/ui/common/ProgressDrawable.java +++ b/app/src/main/java/us/shandian/giga/ui/common/ProgressDrawable.java @@ -16,7 +16,7 @@ public ProgressDrawable(Context context, int background, int foreground) { this(context.getResources().getColor(background), context.getResources().getColor(foreground)); } - public ProgressDrawable(int background, int foreground) { + private ProgressDrawable(int background, int foreground) { mBackgroundColor = background; mForegroundColor = foreground; } diff --git a/app/src/main/java/us/shandian/giga/ui/main/MainActivity.java b/app/src/main/java/us/shandian/giga/ui/main/MainActivity.java index 70c7192..d36873b 100644 --- a/app/src/main/java/us/shandian/giga/ui/main/MainActivity.java +++ b/app/src/main/java/us/shandian/giga/ui/main/MainActivity.java @@ -5,6 +5,7 @@ import android.app.FragmentTransaction; import android.content.ComponentName; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.ServiceConnection; import android.content.SharedPreferences; @@ -15,7 +16,9 @@ import android.os.Bundle; import android.os.IBinder; import android.text.Editable; +import android.text.InputType; import android.text.TextWatcher; +import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MenuItem; @@ -34,14 +37,21 @@ import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.StringReader; import java.net.URL; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.util.Arrays; +import java.util.List; +import java.util.Stack; +import java.util.concurrent.ExecutionException; import us.shandian.giga.R; import us.shandian.giga.get.DownloadManager; +import us.shandian.giga.get.DownloadMission; import us.shandian.giga.service.DownloadManagerService; import us.shandian.giga.ui.adapter.NavigationAdapter; import us.shandian.giga.ui.common.FloatingActionButton; @@ -55,366 +65,474 @@ import us.shandian.giga.util.CrashHandler; import us.shandian.giga.util.Utility; -public class MainActivity extends ToolbarActivity implements AdapterView.OnItemClickListener -{ - public static final String INTENT_DOWNLOAD = "us.shandian.giga.intent.DOWNLOAD"; - - private MissionsFragment mFragment; - private DrawerLayout mDrawer; - private ListView mList; - private NavigationAdapter mAdapter; - private ActionBarDrawerToggle mToggle; - private DownloadManager mManager; - private DownloadManagerService.DMBinder mBinder; - - private String mPendingUrl; - private SharedPreferences mPrefs; - private int mSelection = 0; - - private ServiceConnection mConnection = new ServiceConnection() { - - @Override - public void onServiceConnected(ComponentName p1, IBinder binder) { - mBinder = (DownloadManagerService.DMBinder) binder; - mManager = mBinder.getDownloadManager(); - } - - @Override - public void onServiceDisconnected(ComponentName p1) { - - } - - - }; - - @Override - @TargetApi(21) - protected void onCreate(Bundle savedInstanceState) { - CrashHandler.init(this); - CrashHandler.register(); - - // Service - Intent i = new Intent(); - i.setClass(this, DownloadManagerService.class); - startService(i); - bindService(i, mConnection, Context.BIND_AUTO_CREATE); - - super.onCreate(savedInstanceState); - getSupportActionBar().setDisplayUseLogoEnabled(false); - - mPrefs = getSharedPreferences("threads", Context.MODE_WORLD_READABLE); - - // Drawer - mDrawer = Utility.findViewById(this, R.id.drawer); - mToggle = new ActionBarDrawerToggle(this, mDrawer, mToolbar, 0, 0); - mToggle.setDrawerIndicatorEnabled(true); - mDrawer.setDrawerListener(mToggle); - - if (Build.VERSION.SDK_INT >= 21) { - findViewById(R.id.nav).setElevation(20.0f); - } else { - mDrawer.setDrawerShadow(R.drawable.drawer_shadow, Gravity.LEFT); - } - - mList = Utility.findViewById(this, R.id.nav_list); - mAdapter = new NavigationAdapter(this, R.array.drawer_items, R.array.drawer_icons); - mList.setAdapter(mAdapter); - - // FAB - new FloatingActionButton.Builder(this) - .withButtonColor(getResources().getColor(R.color.blue)) - .withButtonSize(80) - .withDrawable(getResources().getDrawable(R.drawable.ic_add_white_24dp)) - .withGravity(Gravity.RIGHT | Gravity.BOTTOM) - .withPaddings(0, 0, 10, 10) - .create() - .setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (mFragment != null) { - showUrlDialog(); - } - } - }); - - // Fragment - getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - updateFragments(); - getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(this); - } - }); - mList.setOnItemClickListener(this); - - // Intent - if (getIntent().getAction().equals(INTENT_DOWNLOAD)) { - mPendingUrl = getIntent().getData().toString(); - } - } - - - @Override - protected void onNewIntent(Intent intent) { - super.onNewIntent(intent); - - if (intent.getAction().equals(INTENT_DOWNLOAD)) { - mPendingUrl = intent.getData().toString(); - } - } - - @Override - protected void onResume() { - super.onResume(); - - if (mPendingUrl != null) { - showUrlDialog(); - mPendingUrl = null; - } - } - - @Override - protected int getLayoutResource() { - return R.layout.main; - } - - @Override - public void onPostCreate(Bundle savedInstanceState) { - super.onPostCreate(savedInstanceState); - mToggle.syncState(); - } - - @Override - public void onConfigurationChanged(Configuration newConfig) { - super.onConfigurationChanged(newConfig); - mToggle.onConfigurationChanged(newConfig); - } - - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - mDrawer.closeDrawer(Gravity.LEFT); - if (position < 3) { - if (position != mSelection) { - mSelection = position; - updateFragments(); - } - } else if (position == 4) { - Intent i = new Intent(); - i.setAction(Intent.ACTION_VIEW); - i.setClass(this, BrowserActivity.class); - startActivity(i); - } else if (position == 5) { - Intent i = new Intent(); - i.setAction(Intent.ACTION_VIEW); - i.setClass(this, SettingsActivity.class); - startActivity(i); - } - } - - private void updateFragments() { - switch (mSelection) { - case 0: - mFragment = new AllMissionsFragment(); - break; - case 1: - mFragment = new DownloadingMissionsFragment(); - break; - case 2: - mFragment = new DownloadedMissionsFragment(); - break; - } - getFragmentManager().beginTransaction() - .replace(R.id.frame, mFragment) - .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) - .commit(); - - for (int i = 0; i < 3; i++) { - View v = mList.getChildAt(i); - - ImageView icon = Utility.findViewById(v, R.id.drawer_icon); - TextView text = Utility.findViewById(v, R.id.drawer_text); - - if (i == mSelection) { - v.setBackgroundResource(R.color.light_gray); - icon.setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.SRC_IN); - text.setTextColor(getResources().getColor(R.color.blue)); - } else { - v.setBackgroundResource(android.R.color.transparent); - icon.setColorFilter(null); - text.setTextColor(getResources().getColor(R.color.gray)); - } - } - } - - private void showUrlDialog() { - // Create the view - LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); - View v = inflater.inflate(R.layout.dialog_url, null); - final EditText text = Utility.findViewById(v, R.id.url); - final EditText name = Utility.findViewById(v, R.id.file_name); - final TextView tCount = Utility.findViewById(v, R.id.threads_count); - final SeekBar threads = Utility.findViewById(v, R.id.threads); - final Toolbar toolbar = Utility.findViewById(v, R.id.toolbar); - final Button fetch = Utility.findViewById(v, R.id.fetch_name); - - threads.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { - - @Override - public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) { - tCount.setText(String.valueOf(progress + 1)); - } - - @Override - public void onStartTrackingTouch(SeekBar p1) { - - } - - @Override - public void onStopTrackingTouch(SeekBar p1) { - - } - - }); - - int def = mPrefs.getInt("threads", 4); - threads.setProgress(def - 1); - tCount.setText(String.valueOf(def)); - - text.addTextChangedListener(new TextWatcher() { - - @Override - public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4) { - - } - - @Override - public void onTextChanged(CharSequence p1, int p2, int p3, int p4) { - - String url = text.getText().toString().trim(); - - if (!url.equals("")) { - int index = url.lastIndexOf("/"); - - if (index > 0) { - int end = url.lastIndexOf("?"); - - if (end < index) { - end = url.length(); - } - - name.setText(url.substring(index + 1, end)); - } - } - } - - @Override - public void afterTextChanged(Editable txt) { - - } - }); - - if (mPendingUrl != null) { - text.setText(mPendingUrl); - } - - toolbar.setTitle(R.string.add); - toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha); - toolbar.inflateMenu(R.menu.dialog_url); - - // Show the dialog - final AlertDialog dialog = new AlertDialog.Builder(this) - .setCancelable(true) - .setView(v) - .create(); - - dialog.show(); - - fetch.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - new NameFetcherTask().execute(text, name); - } - }); - - toolbar.setNavigationOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dialog.dismiss(); - } - }); - - toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { - @Override - public boolean onMenuItemClick(MenuItem item) { - if (item.getItemId() == R.id.okay) { - String url = text.getText().toString().trim(); - String fName = name.getText().toString().trim(); - - File f = new File(mManager.getLocation() + "/" + fName); - - if (f.exists()) { - Toast.makeText(MainActivity.this, R.string.msg_exists, Toast.LENGTH_SHORT).show(); - } else if (!checkURL(url)) { - Toast.makeText(MainActivity.this, R.string.msg_url_malform, Toast.LENGTH_SHORT).show(); - } else { - - while (mBinder == null); - - int res = mManager.startMission(url, fName, threads.getProgress() + 1); - mBinder.onMissionAdded(mManager.getMission(res)); - mFragment.notifyChange(); - - mPrefs.edit().putInt("threads", threads.getProgress() + 1).commit(); - dialog.dismiss(); - } - - return true; - } else { - return false; - } - } - }); - - } - - private boolean checkURL(String url) { - try { - URL u = new URL(url); - u.openConnection(); - return true; - } catch (MalformedURLException e) { - return false; - } catch (IOException e) { - return false; - } - } - - private class NameFetcherTask extends AsyncTask { - - @Override - protected Object[] doInBackground(View[] params) { - try { - URL url = new URL(((EditText) params[0]).getText().toString()); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - String header = conn.getHeaderField("Content-Disposition"); - - if (header != null && header.indexOf("=") != -1) { - return new Object[]{params[1], header.split("=")[1].replace("\"", "")}; - } - } catch (Exception e) { - - } - return null; - } - - @Override - protected void onPostExecute(Object[] result) { - super.onPostExecute(result); - - if (result != null) { - ((EditText) result[0]).setText(result[1].toString()); - } - } - } +public class MainActivity extends ToolbarActivity implements AdapterView.OnItemClickListener { + public static final String INTENT_DOWNLOAD = "us.shandian.giga.intent.DOWNLOAD"; + + private MissionsFragment mFragment; + private DrawerLayout mDrawer; + private ListView mList; + private NavigationAdapter mAdapter; + private ActionBarDrawerToggle mToggle; + private DownloadManager mManager; + private DownloadManagerService.DMBinder mBinder; + private int numberofDownloads = 2; + + private String mPendingUrl; + private SharedPreferences mPrefs; + private int mSelection = 0; + private int totalBulkDownloads = 0; + private Stack url_stack; + private ServiceConnection mConnection = new ServiceConnection() { + + @Override + public void onServiceConnected(ComponentName p1, IBinder binder) { + mBinder = (DownloadManagerService.DMBinder) binder; + mManager = mBinder.getDownloadManager(); + } + + @Override + public void onServiceDisconnected(ComponentName p1) { + + } + + + }; + + @Override + @TargetApi(21) + protected void onCreate(Bundle savedInstanceState) { + CrashHandler.init(this); + CrashHandler.register(); + + // Service + Intent i = new Intent(); + i.setClass(this, DownloadManagerService.class); + startService(i); + bindService(i, mConnection, Context.BIND_AUTO_CREATE); + + super.onCreate(savedInstanceState); + getSupportActionBar().setDisplayUseLogoEnabled(false); + + mPrefs = getSharedPreferences("threads", Context.MODE_WORLD_READABLE); + + // Drawer + mDrawer = Utility.findViewById(this, R.id.drawer); + mToggle = new ActionBarDrawerToggle(this, mDrawer, mToolbar, 0, 0); + mToggle.setDrawerIndicatorEnabled(true); + mDrawer.setDrawerListener(mToggle); + + if (Build.VERSION.SDK_INT >= 21) { + findViewById(R.id.nav).setElevation(20.0f); + } else { + mDrawer.setDrawerShadow(R.drawable.drawer_shadow, Gravity.LEFT); + } + + mList = Utility.findViewById(this, R.id.nav_list); + mAdapter = new NavigationAdapter(this, R.array.drawer_items, R.array.drawer_icons); + mList.setAdapter(mAdapter); + + // FAB + new FloatingActionButton.Builder(this) + .withButtonColor(getResources().getColor(R.color.blue)) + .withButtonSize(80) + .withDrawable(getResources().getDrawable(R.drawable.ic_add_white_24dp)) + .withGravity(Gravity.RIGHT | Gravity.BOTTOM) + .withPaddings(0, 0, 10, 10) + .create() + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mFragment != null) { + showUrlDialog(); + } + } + }); + + // Fragment + getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + updateFragments(); + getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(this); + } + }); + mList.setOnItemClickListener(this); + + // Intent + if (getIntent().getAction().equals(INTENT_DOWNLOAD)) { + mPendingUrl = getIntent().getData().toString(); + } + } + + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + + if (intent.getAction().equals(INTENT_DOWNLOAD)) { + mPendingUrl = intent.getData().toString(); + } + } + + @Override + protected void onResume() { + super.onResume(); + + if (mPendingUrl != null) { + showUrlDialog(); + mPendingUrl = null; + } + } + + @Override + protected int getLayoutResource() { + return R.layout.main; + } + + @Override + public void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + mToggle.syncState(); + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + mToggle.onConfigurationChanged(newConfig); + } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + mDrawer.closeDrawer(Gravity.LEFT); + List drawer_items = Arrays.asList(getResources().getStringArray(R.array.drawer_items)); + if (position < 3) { + if (position != mSelection) { + mSelection = position; + updateFragments(); + } + } else if (position == drawer_items.indexOf("Browser")) { //Browser + Intent i = new Intent(); + i.setAction(Intent.ACTION_VIEW); + i.setClass(this, BrowserActivity.class); + startActivity(i); + } else if (position == drawer_items.indexOf("Embedded Browser")) { //Settings + Intent i = new Intent(); + i.setAction(Intent.ACTION_VIEW); + i.setClass(this, SettingsActivity.class); + startActivity(i); + } else if (position == drawer_items.indexOf("Add Bulk")) { + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("Add links to new lines"); + final EditText input = new EditText(this); + input.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE); + input.setMinLines(4); + input.setSingleLine(false); + input.setHorizontallyScrolling(true); + builder.setView(input); + builder.setPositiveButton("Start", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + url_stack = new Stack(); + String text = input.getText().toString(); + BufferedReader bufReader = new BufferedReader(new StringReader(text)); + String line = null; + url_stack = new Stack(); + try { + while ((line = bufReader.readLine()) != null) { + url_stack.add(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + totalBulkDownloads = url_stack.size(); + for (int i = 0; i < numberofDownloads; i++) + startQue(); + } + }); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() + + { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + } + ); + builder.show(); + } + } + + + private void startQue() { + if (url_stack.isEmpty()) { + Toast.makeText(MainActivity.this, "Task Completed", Toast.LENGTH_LONG).show(); + return; + } + Toast.makeText(MainActivity.this, "Downloading File " + url_stack.size() + "/" + totalBulkDownloads, Toast.LENGTH_SHORT).show(); + String url = ""; + getFileName asd; + try { + url = url_stack.pop(); + Log.d("Stacked", "url-" + url); + String header = ""; + asd = new getFileName(); + header = asd.execute(url).get(); + + while (mBinder == null) ; + int res = mManager.startMission(url, header, 1); + DownloadMission mission = mManager.getMission(res); + mBinder.onMissionAdded(mManager.getMission(res)); + mFragment.notifyChange(); + mPrefs.edit().putInt("threads", 1).commit(); + mission.addListener(new DownloadMission.MissionListener() { + @Override + public void onProgressUpdate(long done, long total) { + + } + + @Override + public void onFinish() { + startQue(); + } + + @Override + public void onError(int errCode) { + startQue(); + } + }); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + } + + private class getFileName extends AsyncTask { + @Override + protected String doInBackground(String... strings) { + String header = ""; + try { + URL url = new URL(strings[0]); + HttpURLConnection conn = null; + conn = (HttpURLConnection) url.openConnection(); + header = conn.getHeaderField("Content-Disposition"); + if (header != null && header.indexOf("=") != -1) { + header = header.split("=")[1].replace("\"", ""); + } + + conn.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + } + return header; + } + } + + private void updateFragments() { + switch (mSelection) { + case 0: + mFragment = new AllMissionsFragment(); + break; + case 1: + mFragment = new DownloadingMissionsFragment(); + break; + case 2: + mFragment = new DownloadedMissionsFragment(); + break; + } + getFragmentManager().beginTransaction() + .replace(R.id.frame, mFragment) + .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) + .commit(); + + for (int i = 0; i < 3; i++) { + View v = mList.getChildAt(i); + + ImageView icon = Utility.findViewById(v, R.id.drawer_icon); + TextView text = Utility.findViewById(v, R.id.drawer_text); + + if (i == mSelection) { + v.setBackgroundResource(R.color.light_gray); + icon.setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.SRC_IN); + text.setTextColor(getResources().getColor(R.color.blue)); + } else { + v.setBackgroundResource(android.R.color.transparent); + icon.setColorFilter(null); + text.setTextColor(getResources().getColor(R.color.gray)); + } + } + } + + private void showUrlDialog() { + // Create the view + LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View v = inflater.inflate(R.layout.dialog_url, null); + final EditText text = Utility.findViewById(v, R.id.url); + final EditText name = Utility.findViewById(v, R.id.file_name); + final TextView tCount = Utility.findViewById(v, R.id.threads_count); + final SeekBar threads = Utility.findViewById(v, R.id.threads); + final Toolbar toolbar = Utility.findViewById(v, R.id.toolbar); + final Button fetch = Utility.findViewById(v, R.id.fetch_name); + + threads.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + + @Override + public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) { + tCount.setText(String.valueOf(progress + 1)); + } + + @Override + public void onStartTrackingTouch(SeekBar p1) { + + } + + @Override + public void onStopTrackingTouch(SeekBar p1) { + + } + + }); + + int def = mPrefs.getInt("threads", 4); + threads.setProgress(def - 1); + tCount.setText(String.valueOf(def)); + + text.addTextChangedListener(new TextWatcher() { + + @Override + public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4) { + + } + + @Override + public void onTextChanged(CharSequence p1, int p2, int p3, int p4) { + + String url = text.getText().toString().trim(); + + if (!url.equals("")) { + int index = url.lastIndexOf("/"); + + if (index > 0) { + int end = url.lastIndexOf("?"); + + if (end < index) { + end = url.length(); + } + + name.setText(url.substring(index + 1, end)); + } + } + } + + @Override + public void afterTextChanged(Editable txt) { + + } + }); + + if (mPendingUrl != null) { + text.setText(mPendingUrl); + } + + toolbar.setTitle(R.string.add); + toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha); + toolbar.inflateMenu(R.menu.dialog_url); + + // Show the dialog + final AlertDialog dialog = new AlertDialog.Builder(this) + .setCancelable(true) + .setView(v) + .create(); + + dialog.show(); + + fetch.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + new NameFetcherTask().execute(text, name); + } + }); + + toolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dialog.dismiss(); + } + }); + + toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + if (item.getItemId() == R.id.okay) { + String url = text.getText().toString().trim(); + String fName = name.getText().toString().trim(); + + File f = new File(mManager.getLocation() + "/" + fName); + + if (f.exists()) { + Toast.makeText(MainActivity.this, R.string.msg_exists, Toast.LENGTH_SHORT).show(); + } else if (!checkURL(url)) { + Toast.makeText(MainActivity.this, R.string.msg_url_malform, Toast.LENGTH_SHORT).show(); + } else { + + while (mBinder == null) ; + + int res = mManager.startMission(url, fName, threads.getProgress() + 1); + mBinder.onMissionAdded(mManager.getMission(res)); + mFragment.notifyChange(); + + mPrefs.edit().putInt("threads", threads.getProgress() + 1).commit(); + dialog.dismiss(); + } + + return true; + } else { + return false; + } + } + }); + + } + + private boolean checkURL(String url) { + try { + URL u = new URL(url); + u.openConnection(); + return true; + } catch (MalformedURLException e) { + return false; + } catch (IOException e) { + return false; + } + } + + private class NameFetcherTask extends AsyncTask { + + @Override + protected Object[] doInBackground(View[] params) { + try { + URL url = new URL(((EditText) params[0]).getText().toString()); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + String header = conn.getHeaderField("Content-Disposition"); + + if (header != null && header.indexOf("=") != -1) { + return new Object[]{params[1], header.split("=")[1].replace("\"", "")}; + } + } catch (Exception e) { + Log.d("Exception", e.toString()); + } + return null; + } + + @Override + protected void onPostExecute(Object[] result) { + super.onPostExecute(result); + + if (result != null) { + ((EditText) result[0]).setText(result[1].toString()); + } + } + } } diff --git a/app/src/main/java/us/shandian/giga/ui/web/BrowserActivity.java b/app/src/main/java/us/shandian/giga/ui/web/BrowserActivity.java index a38fda9..997275e 100755 --- a/app/src/main/java/us/shandian/giga/ui/web/BrowserActivity.java +++ b/app/src/main/java/us/shandian/giga/ui/web/BrowserActivity.java @@ -6,7 +6,6 @@ import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; -import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.util.Log; diff --git a/app/src/main/java/us/shandian/giga/util/CrashHandler.java b/app/src/main/java/us/shandian/giga/util/CrashHandler.java index 5404e0d..088156a 100644 --- a/app/src/main/java/us/shandian/giga/util/CrashHandler.java +++ b/app/src/main/java/us/shandian/giga/util/CrashHandler.java @@ -10,15 +10,15 @@ public class CrashHandler implements Thread.UncaughtExceptionHandler { - public static String CRASH_DIR = Environment.getExternalStorageDirectory().getPath() + "/GigaCrash/"; - public static String CRASH_LOG = CRASH_DIR + "last_crash.log"; - public static String CRASH_TAG = CRASH_DIR + ".crashed"; + private static String CRASH_DIR = Environment.getExternalStorageDirectory().getPath() + "/GigaCrash/"; + private static String CRASH_LOG = CRASH_DIR + "last_crash.log"; + private static String CRASH_TAG = CRASH_DIR + ".crashed"; private static String ANDROID = Build.VERSION.RELEASE; private static String MODEL = Build.MODEL; private static String MANUFACTURER = Build.MANUFACTURER; - public static String VERSION = "Unknown"; + private static String VERSION = "Unknown"; private Thread.UncaughtExceptionHandler mPrevious; diff --git a/app/src/main/java/us/shandian/giga/util/Settings.java b/app/src/main/java/us/shandian/giga/util/Settings.java index 82ceb28..b406732 100644 --- a/app/src/main/java/us/shandian/giga/util/Settings.java +++ b/app/src/main/java/us/shandian/giga/util/Settings.java @@ -8,7 +8,7 @@ */ public class Settings { - public static final String XML_NAME = "settings"; + private static final String XML_NAME = "settings"; public static final String DOWNLOAD_DIRECTORY = "download_directory"; diff --git a/app/src/main/java/us/shandian/giga/util/Utility.java b/app/src/main/java/us/shandian/giga/util/Utility.java index 3bfbea1..4072111 100644 --- a/app/src/main/java/us/shandian/giga/util/Utility.java +++ b/app/src/main/java/us/shandian/giga/util/Utility.java @@ -71,7 +71,7 @@ public static void writeToFile(String fileName, String content) { } } - public static void writeToFile(String fileName, byte[] content) { + private static void writeToFile(String fileName, byte[] content) { File f = new File(fileName); if (!f.exists()) { @@ -256,12 +256,12 @@ public static int getIconForFileType(FileType type) { } } - public static boolean isDirectoryAvailble(String path) { + private static boolean isDirectoryAvailble(String path) { File dir = new File(path); return dir.exists() && dir.isDirectory(); } - public static boolean isDownloadDirectoryAvailble(Context context) { + private static boolean isDownloadDirectoryAvailble(Context context) { return isDirectoryAvailble(Settings.getInstance(context).getString(Settings.DOWNLOAD_DIRECTORY, Settings.DEFAULT_PATH)); } @@ -294,7 +294,7 @@ public static void processDirectoryChange(int requestCode, int resultCode, //remove "file://" from the path } - public static void checkAndReshow(Activity activity){ + private static void checkAndReshow(Activity activity){ if (!isDownloadDirectoryAvailble(activity)){ Toast.makeText(activity.getApplicationContext(), R.string.no_available_dir, Toast.LENGTH_LONG).show(); diff --git a/app/src/main/res/values-zh-rCN/arrays.xml b/app/src/main/res/values-zh-rCN/arrays.xml index f801df9..2e112da 100644 --- a/app/src/main/res/values-zh-rCN/arrays.xml +++ b/app/src/main/res/values-zh-rCN/arrays.xml @@ -5,6 +5,7 @@ 正在下载 已完成 - + 批量添加 内置浏览器 设置 diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 87f1f71..c3039e4 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -4,15 +4,18 @@ All Downloading Done + Add Bulk - Embedded Browser Settings + all downloading done + downloading null browser settings diff --git a/build.gradle b/build.gradle index 50e899c..dd27eef 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.0.0' + classpath 'com.android.tools.build:gradle:1.5.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files