Esempio n. 1
0
 def insert(self, file_path, root_dir='*', sub_dir='', volume='external'):
     success = False
     try:
         volume = volume.lower()
         if volume not in ['internal', 'external']:
             volume = 'external'
         file_name = basename(file_path)
         # delete existing
         self.delete(file_name, root_dir, sub_dir, volume)
         # build MediaStore data
         MIME_type = self._get_file_MIME_type(file_name)
         root_directory = self._get_root_directory(root_dir, MIME_type)
         sub_directory = join(root_directory, self._app_name())
         if sub_dir:
             sub_dirs = sub_dir.split('/')
             for s in sub_dirs:
                 sub_directory = join(sub_directory, s)
         root_uri = self._get_root_uri(root_directory, volume)
         cv = ContentValues()
         cv.put(MediaStoreMediaColumns.DISPLAY_NAME, file_name)
         cv.put(MediaStoreMediaColumns.MIME_TYPE, MIME_type)
         cv.put(MediaStoreMediaColumns.RELATIVE_PATH, sub_directory)
         # copy file
         context = mActivity.getApplicationContext()
         uri = context.getContentResolver().insert(root_uri, cv)
         ws = context.getContentResolver().openOutputStream(uri)
         rs = FileInputStream(file_path)
         FileUtils.copy(rs, ws)
         ws.flush()
         ws.close()
         rs.close()
         success = bool(self.getUri(file_name, root_dir, sub_dir, volume))
     except Exception as e:
         print('ERROR SharedStorage.insert():\n' + str(e))
     return success
Esempio n. 2
0
 def _app_name(self):
     context = mActivity.getApplicationContext()
     appinfo = context.getApplicationInfo()
     if appinfo.labelRes:
         name = context.getString(appinfo.labelRes)
     else:
         name = appinfo.nonLocalizedLabel.toString()
     return name
Esempio n. 3
0
    def is_location_enabled(self):
        context = cast('android.content.Context',
                       mActivity.getApplicationContext())

        locationManager = cast(
            'android.location.LocationManager',
            context.getSystemService(Context.LOCATION_SERVICE))

        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
Esempio n. 4
0
 def delete(self, file_name, root_dir='*', sub_dir='', volume='external'):
     success = False
     try:
         fileUri = self.getUri(file_name, root_dir, sub_dir, volume)
         if fileUri:
             context = mActivity.getApplicationContext()
             context.getContentResolver().delete(fileUri, None, None)
             success = True
     except Exception as e:
         print('ERROR SharedStorage.delete():\n' + str(e))
     return success
Esempio n. 5
0
 def getCacheDir(self, volume='external'):
     if volume not in ['internal', 'external']:
         volume = 'external'
     context = mActivity.getApplicationContext()
     result = ''
     if volume == 'internal':
         result = context.getCacheDir().toString()
     else:
         result = context.getExternalCacheDir()
         if result:
             result = result.toString()
     return str(result)
 def turn_on_gps(self, caller=''):
     if platform == 'android':
         context = cast(
             'android.content.Context',
             mActivity.getApplicationContext())
         locationManager = cast(
             'android.location.LocationManager',
             context.getSystemService(Context.LOCATION_SERVICE))
         if locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER):
             if not caller:
                 toast('GPS on', 3)
         else:
             self.dialog.open()
Esempio n. 7
0
    def getUri(self,
               file_name,
               root_dir='*',
               sub_dir='',
               volume='external',
               this_app=True):
        fileUri = None
        try:
            volume = volume.lower()
            if volume not in ['internal', 'external']:
                volume = 'external'
            MIME_type = self._get_file_MIME_type(file_name)
            root_directory = self._get_root_directory(root_dir, MIME_type)
            root_uri = self._get_root_uri(root_directory, volume)
            location = root_directory
            if this_app:
                location = join(location, self._app_name())
            if sub_dir:
                sub_dirs = sub_dir.split('/')
                for s in sub_dirs:
                    location = join(location, s)
            location = join(location, "")

            selection = MediaStoreMediaColumns.DISPLAY_NAME+" LIKE '"+\
                file_name+"'"
            selection += " AND "
            selection += MediaStoreMediaColumns.RELATIVE_PATH+" LIKE '"+\
                location+"'"

            context = mActivity.getApplicationContext()
            cursor = context.getContentResolver().query(
                root_uri, None, selection, None, None)
            while cursor.moveToNext():
                dn = MediaStoreMediaColumns.DISPLAY_NAME
                rp = MediaStoreMediaColumns.RELATIVE_PATH
                index = cursor.getColumnIndex(dn)
                fileName = cursor.getString(index)
                dindex = cursor.getColumnIndex(rp)
                dName = cursor.getString(dindex)
                if file_name == fileName:
                    id_index = cursor.getColumnIndex(
                        MediaStoreMediaColumns._ID)
                    id = cursor.getLong(id_index)
                    fileUri = ContentUris.withAppendedId(root_uri, id)
                    break
            cursor.close()
        except Exception as e:
            print('ERROR SharedStorage.getUri():\n' + str(e))
        return fileUri
Esempio n. 8
0
 def retrieveUri(self, someUri):
     new_file_path = ''
     try:
         if someUri:
             someUri = cast('android.net.Uri', someUri)
             scheme = someUri.getScheme().lower()
             if scheme == 'content':
                 context = mActivity.getApplicationContext()
                 cursor = context.getContentResolver().query(
                     someUri, None, None, None, None)
                 dn = MediaStoreMediaColumns.DISPLAY_NAME
                 nameIndex = cursor.getColumnIndex(dn)
                 cursor.moveToFirst()
                 file_name = cursor.getString(nameIndex)
                 cursor.close()
             elif scheme == 'file':
                 file_name = basename(someUri.getPath())
             else:
                 #https://en.wikipedia.org/wiki/List_of_URI_schemes
                 return someUri.toString()
             # Save to
             new_file_path = PrivateStorage().getCacheDir()
             if not new_file_path:
                 new_file_path = PrivateStorage().getCacheDir('internal')
             new_file_path = join(new_file_path, "MediaStore")
             if not exists(new_file_path):
                 mkdir(new_file_path)
             new_file_path = join(new_file_path, file_name)
             # Copy
             rs = mActivity.getContentResolver().openInputStream(someUri)
             ws = FileOutputStream(new_file_path)
             FileUtils.copy(rs, ws)
             ws.flush()
             ws.close()
             rs.close()
     except Exception as e:
         print('ERROR SharedStorage.retrieveUri():\n' + str(e))
     return new_file_path