Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StoragePathFragment.java « settings « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c1d889426f4ec197901d880a20e34a6c54db3c3 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.mapswithme.maps.settings;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.mapswithme.maps.R;
import com.mapswithme.maps.base.OnBackPressListener;
import com.mapswithme.util.Constants;
import com.mapswithme.util.Utils;

import java.util.List;
import java.util.Locale;

public class StoragePathFragment extends BaseSettingsFragment
                              implements StoragePathManager.MoveFilesListener,
                                         OnBackPressListener
{
  private TextView mHeader;
  private ListView mList;

  private StoragePathAdapter mAdapter;
  private final StoragePathManager mPathManager = new StoragePathManager();

  @Override
  protected int getLayoutRes()
  {
    return R.layout.fragment_prefs_storage;
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
    View root = super.onCreateView(inflater, container, savedInstanceState);

    mHeader = (TextView) root.findViewById(R.id.header);
    mList = (ListView) root.findViewById(R.id.list);
    mList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id)
      {
        mAdapter.onItemClick(position);
      }
    });

    return root;
  }

  @Override
  public void onResume()
  {
    super.onResume();
    mPathManager.startExternalStorageWatching(getActivity(), new StoragePathManager.OnStorageListChangedListener()
    {
      @Override
      public void onStorageListChanged(List<StorageItem> storageItems, int currentStorageIndex)
      {
        updateList();
      }
    }, this);

    if (mAdapter == null)
      mAdapter = new StoragePathAdapter(mPathManager, getActivity());

    updateList();
    mList.setAdapter(mAdapter);
  }

  @Override
  public void onPause()
  {
    super.onPause();
    mPathManager.stopExternalStorageWatching();
  }

  private void updateList()
  {
    long dirSize = StorageUtils.getWritableDirSize();
    mHeader.setText(getString(R.string.maps) + ": " + getSizeString(dirSize));

    if (mAdapter != null)
      mAdapter.update(mPathManager.getStorageItems(), mPathManager.getCurrentStorageIndex(), dirSize);
  }

  @Override
  public void moveFilesFinished(String newPath)
  {
    updateList();
  }

  @Override
  public void moveFilesFailed(int errorCode)
  {
    if (!isAdded())
      return;

    final String message = "Failed to move maps with internal error: " + errorCode;
    final Activity activity = getActivity();
    if (activity.isFinishing())
      return;

    new AlertDialog.Builder(activity)
        .setTitle(message)
        .setPositiveButton(R.string.report_a_bug,
                           (dialog, which) -> Utils.sendBugReport(activity, message)).show();
  }

  static String getSizeString(long size)
  {
    final String units[] = { "Kb", "Mb", "Gb" };

    long current = Constants.KB;
    int i = 0;
    for (; i < units.length; ++i)
    {
      final long bound = Constants.KB * current;
      if (size < bound)
        break;

      current = bound;
    }

    // left 1 digit after the comma and add postfix string
    return String.format(Locale.US, "%.1f %s", (double) size / current, units[i]);
  }

  @Override
  public boolean onBackPressed()
  {
    return false;
  }
}